python2 字符串unicode str编码解码问题

版权声明:本文为博主原创文章,转载需要注明源地址 https://blog.csdn.net/qq_33512078/article/details/78627944

若在python2文件中硬编码一个中文字符串(python2文件编码设为utf-8),
其类型为一个str变量,可以使用decode('utf-8')方法将其转化为unicode变量

a = '测试'
type(a)
Out[1]: str
a.decode('utf-8')
Out[2]: u'\u6d4b\u8bd5'
a.encode('utf-8')
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/IPython/core/interactiveshell.py", line 2882, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-7-e6028649de51>", line 1, in <module>
    a.encode('utf-8')
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe6 in position 0: ordinal not in range(128)

python中字符串有以下转化关系

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

猜你喜欢

转载自blog.csdn.net/qq_33512078/article/details/78627944