Python中的UTF-32

rod*_*ing 2 python unicode

我无法显示unicode项目u'\u201d'.我没有其他unicode项目的问题.我使用了UTF-8,但是这个角色在我的代码上出现并下了地狱.我在翻译中尝试了不同的东西.但基本上在哪里:

c = u'\u201d'
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

Traceback (most recent call last):
File "<pyshell#154>", line 1, in <module>
    c.decode('utf-32')
  File "C:\Python27\lib\encodings\utf_32.py", line 11, in decode
    return codecs.utf_32_decode(input, errors, True)
UnicodeEncodeError: 'ascii' codec can't encode character u'\u201d' in position 0: ordinal not in range(128)
Run Code Online (Sandbox Code Playgroud)

我需要在GUI中显示它,以便检查输出,然后将其存储为纯文本. 在python中转换unicode字符串 解释了一下,但是我仍然明显遗漏了一些东西.

akg*_*ood 6

如果您收到此异常,那么您正在尝试调用.decode()unicode字符串.您应该只调用.decode()字节字符串,并且只调用.encode()unicode字符串.否则,解释器将首先使用默认编解码器(通常为"ascii")隐式编码或解码字符串,这是坏消息.

一般来说,我建议仔细阅读http://farmdev.com/talks/unicode/ ...

  • 定义"纯文本".在这种情况下,没有"纯文本"这样的东西.我建议阅读[绝对最低每个软件开发人员,绝对必须知道关于Unicode和字符集](http://www.joelonsoftware.com/articles/Unicode.html) (3认同)