我有一个列表 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']

谢谢!

最佳答案

一个便宜的解决方案

print n.encode('ascii','backslashreplace')

或者
print n.encode('ascii','ignore')

但最好查看 Martijn Pieters 链接并修复编码……否则您的程序中其他地方可能会遇到更多问题

关于python - 使用 '\xae' 转义带有元素的列表并打印结果,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16179588/

10-11 18:18