我有一个清单L = [u'steve', u'micheal', u'pedro\xae']
当我尝试阅读它时,出现错误,我相信它与 '\xae' 有关
>>> L = [u'steve', u'micheal', u'pedro\xae']
>>>
>>> for n in L:
... print n
...
steve
micheal
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xae' in position 5: ordinal not in range(128)
>>>
知道如何逃脱这个角色吗?
使读取变得非常简单所需的输出是:
L= ['steve', 'micheal', 'pedro']
Thanks!
一个廉价的解决方案
print n.encode('ascii','backslashreplace')
or
print n.encode('ascii','ignore')
但最好查看 Martijn Pieters 链接并修复编码...否则您的程序中的其他地方可能会遇到更多问题
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)