1. 程式人生 > >python2 字串unicode str編碼解碼問題

python2 字串unicode str編碼解碼問題

若在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