码迷,mamicode.com
首页 > 编程语言 > 详细

Python UnicodeDecodeError

时间:2017-05-20 13:21:48      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:unicode   line   stdin   ace   color   most   python   span   inpu   

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

标签:unicode   line   stdin   ace   color   most   python   span   inpu   

原文地址:http://www.cnblogs.com/IDRI/p/6881910.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!