各位大侠,Python 编码问题
例子是这样的:
>>> str1 = '小狗'
>>> str1
'小狗'
>>> str1.encode('utf-8')
b'\xe5\xb0\x8f\xe7\x8b\x97'
>>> str2 = str1.encode('utf-8')
>>> str2
b'\xe5\xb0\x8f\xe7\x8b\x97'
我要还原为'小狗'用哪种编码格式
注:python 2.4和python 3.1
>>>str1.encode('gbk')
Traceback (most recent call last):
File "<pyshell#6>", line 1, in <module>
str2.encode('gbk')
AttributeError: 'bytes' object has no attribute 'encode'
[解决办法]
str2.decode('utf8')