gpt4 book ai didi

python - 转义包含 '\xae' 元素的列表并打印结果

转载 作者:太空宇宙 更新时间:2023-11-03 13:47:41 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com