我在 Python 中遇到了一个小问题。我无法打印包含“é”字符的字符串。我会解释:
for actor in show.actor_objects:
f.write(u"\n <actor>")
f.write(u"\n <name>{0}</name>".format(str(actor.Name).encode('ascii', 'ignore')))
f.write(u"\n <role>{0}</role>".format(str(actor.Role).encode('ascii', 'ignore')))
f.write(u"\n </actor>")
我收到以下错误消息:
root@vroum:21:26:44#~:?1# python test.py -s 2 -n Kaamelott -o outfile.txt -f 0 -l 50 Traceback (most recent call last):
File "test.py", line 104, in <module>
main(sys.argv[1:])
File "test.py", line 99, in main
f.write(u"\n <role>{0}</role>".format(str(actor.Role).encode('ascii', 'ignore')))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 1: ordinal not in range(128)
我该如何解决这个问题?我正在使用 Python 2.7。
最佳答案
问题是您将 unicode 字符串传递给 str() 函数(在 Python 2 中,其中 str 是字节字符串)。如果您只是摆脱对 str 的调用,它应该可以工作:
f.write(u"\n <name>{0}</name>".format(actor.Name.encode('ascii', 'ignore')))
f.write(u"\n <role>{0}</role>".format(actor.Role.encode('ascii', 'ignore')))
然而,使用 encode('ascii', 'ignore') 将完全删除 unicode 字符。你可能想做这样的事情:
f.write(u"\n <name>{0}</name>".format(actor.Name).encode('UTF-8'))
f.write(u"\n <role>{0}</role>".format(actor.Role).encode('UTF-8'))
关于python - 为什么在使用非 ASCII 字符时无法转换字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24470194/