本文介绍了使用'\ xae'转义带有元素的列表并打印结果的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个列表L = [u'steve', u'micheal', u'pedro\xae']

当我尝试阅读它时,出现错误,我相信它与'\ xae'有关

when I tried to read it, I got an error, I believe it has something to do with the '\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)
>>>

有什么想法逃脱角色吗?

Any idea how to escape the caracter?

所需的输出,因此读数非常简单:

The output desired so the reading will be very simple is :

L= ['steve', 'micheal', 'pedro']

谢谢!

推荐答案

便宜的解决方案

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

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

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

but better to look at Martijn Pieters link and fix the encoding ... or you will likely have more problems elsewhere in your program

这篇关于使用'\ xae'转义带有元素的列表并打印结果的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

11-02 15:52