首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > perl python >

各位大侠,Python 编码有关问题

2012-03-06 
各位大侠,Python 编码问题例子是这样的: str1 小狗 str1小狗 str1.encode(utf-8)b\xe5\

各位大侠,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')

热点排行