【发布时间】:2017-03-08 17:11:40
【问题描述】:
我想让这段代码与 python2-3 兼容:
def normalize_text(text, ignore_characters=''):
if type(ignore_characters) not in [list, str, unicode]:
ignore_characters = ''
if type(ignore_characters) == str:
ignore_characters = ignore_characters.decode('utf-8')
if type(ignore_characters) == list:
new_ignore_characters = []
for item in ignore_characters:
if type(item) == str:
new_ignore_characters.append(item.decode('utf-8'))
elif type(item) == unicode:
new_ignore_characters.append(item)
ignore_characters = new_ignore_characters
if type(text) == str:
text = text.decode('utf-8')
在python 3 中str 类型上没有unicode 或decode。使此代码与python2-3 兼容的最佳解决方法是什么?
【问题讨论】:
标签: python python-3.x unicode compatibility python-2.x