問題是因爲Python2和Python3之間的差異。如果你這樣做在Python3它的工作馬上
Python 3.6.2 (default, Jul 17 2017, 16:44:45)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> value = 'Château'
>>> print (value)
Château
>>> array={'test':value,'test2':'Château'}
>>> print(array)
{'test': 'Château', 'test2': 'Château'}
>>>
現在讓我們回到Python2
Python 2.7.13 (default, Jul 18 2017, 09:17:00)
[GCC 4.2.1 Compatible Apple LLVM 8.1.0 (clang-802.0.42)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> value = 'Château'
>>> print value;
Château
>>> array={'test':value,'test2':'Château'}
>>> print array
{'test': 'Ch\xc3\xa2teau', 'test2': 'Ch\xc3\xa2teau'}
這是因爲當您打印陣列,它被轉換爲字符串表示和蟒蛇不是Unicode
>>> str(array)
"{'test': 'Ch\\xc3\\xa2teau', 'test2': 'Ch\\xc3\\xa2teau'}"
>>> print str(array)
{'test': 'Ch\xc3\xa2teau', 'test2': 'Ch\xc3\xa2teau'}
你想要做什麼,而印刷是做Unicode轉義
>>> print str(array).decode("unicode-escape")
{'test': 'Château', 'test2': 'Château'}
但等待這樣弄亂打印?這是因爲需要打印這些字符的編碼。拉丁語短片
>>> print str(array).decode("unicode-escape").encode("latin-1")
{'test': 'Château', 'test2': 'Château'}
只需升級到python3,您的問題將被排序。但是您需要將打印報表更改爲print(...)
。否則使用代碼來編碼編碼,如我所示
是的。 Python自動完成它。在Python3中,Unicode字符串看起來是正確的 – AndMar
@marni所以在Python 3中它將全部正確嗎? –
是的。但在Py2中它也是正確的(簡而言之:只是視圖不同而已,long:Python 2和3以不同的方式使用Unicode),請不要擔心 – AndMar