首页 > 代码库 > Python UnicodeDecodeError

Python UnicodeDecodeError

UnicodeDecodeError

UnicodeDecodeError: ‘ascii‘ codec can‘t decode byte 0xe9 in position 0: ordinal not in range(128)

(Python2.7)

 

原因:

str  -> decode(the_coding_of_str) -> unicode
unicode -> encode(the_coding_you_want) -> str

unicode encode(编码)后 成『字节』,组成『字节串』,即 str

 

>>> u中文.encode(utf-8)
\xe4\xb8\xad\xe6\x96\x87
>>> len(u中文.encode(utf-8))
6 
>>> type(u中文)
<type unicode>

如果对 str 进行 encode,对 unicode 进行 decode,会报错。

>>> 中文.encode(utf-8)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeDecodeError: ascii codec cant decode byte 0xe4 in position 0: ordinal not in range(128)

>>> u中文.decode(utf-8)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/System/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/encodings/utf_8.py", line 16, in decode
    return codecs.utf_8_decode(input, errors, True)
UnicodeEncodeError: ascii codec cant encode characters in position 0-1: ordinal not in range(128)

 

Python UnicodeDecodeError