でこれを行うにはUTF-8で表示したい場合。あなたはの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'}
>>>
を働くだろうさて、あなたは配列を印刷するとき、それはPythonでのUnicode文字列表現に変換していないので、この問題が発生したバック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'}
>>> 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ではすべて正しいでしょうか? –
はい。 Python 2と3は異なる方法でUnicodeで動作します。 – AndMar