~/download/firefox $ python2
Python 2.7.2 (default, Jun 29 2011, 11:17:09)
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> '张俊'.encode('utf-8') #python2 已经自动将其转化为utf-8类型编码,因此再次编码(python2会将该字符串当作用ascii或unicode编码过)会出现错误。
Traceback (most recent call last):
File "", line 1, in
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe5 in position 0: ordinal not in range(128)
~/download/firefox $ python3
Python 3.2.2 (default, Sep 5 2011, 04:33:58)
[GCC 4.6.1 20110819 (prerelease)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> '张俊'.decode('utf-8') #因为默认的文本字符串为unicode格式,因此文本字符串没有decode方法
Traceback (most recent call last):
File "", line 1, in
AttributeError: 'str' object has no attribute 'decode'