python - 为什么在使用非 ASCII 字符时无法转换字符串?

标签 python string unicode ascii

我在 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/

相关文章:

python - 在 python 中发送命令行命令并打印 stdout 和 stderr 的最简单和最可靠的方法是什么?

python - 为什么我计算数字阶乘的算法的时间复杂度是 O(n^2) 而不是预期的 O(n)?

python - 使图像特定区域的像素空白或用任何颜色填充该区域

javascript - 如何在javascript中转义^字符?

string - 是否有任何流行和/或有效的递归查找和替换算法?

c++ - 如何在不使用 variable_name.at() 的情况下引用字符串的最后一个字符?

MySQL排序存储未知语言的多语言数据

python - Neo4j 处理数据快吗?对我来说太慢了

javascript - 每种语言的字体大小

string - 在 Perl 中,为什么 utf-8 字符串在拆分为字符时打印的不同?