【发布时间】:2015-02-28 12:21:01
【问题描述】:
我希望对项目中的所有字符串都使用unicode 而不是str。我正在尝试使用 str.encode 方法,但无法从文档中确定 encode 方法究竟做了什么或期望作为输入。
希腊小写字母pi为U+03C0,UTF-8编码为0xCF 0x80。我得到以下信息:
>>> s1 = '\xcf\x80'
>>> s1.encode('utf-8','ignore')
Traceback (most recent call last):
File "<pyshell#61>", line 1, in <module>
s1.encode('utf-8','ignore')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xcf in position 0: ordinal not in range(128)
我试过了:
>>> s2='\x03\xc0'
>>> s2.encode('utf-8','ignore')
Traceback (most recent call last):
File "<pyshell#62>", line 1, in <module>
s2.encode('utf-8','ignore')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc0 in position 1: ordinal not in range(128)
encode 期望输入什么,为什么“忽略”选项不忽略错误?我尝试了“替换”,但也没有掩盖错误。
【问题讨论】:
标签: python string python-2.7 encoding character-encoding