如何在python 2.6中打印unicode中文字符串?

时间:2017-06-19 17:42:23

标签: python unicode

这是我试图打印統計情報的中文字符串。我想看unicode表示python控制台。这个字符串在文件中。 所以这就是我的尝试。

import codecs
with codecs.open("testutf8.txt", "r", "utf-8") as f:
     fa=f.read()
     print fa.encode('utf-8')

这仍然在控制台中打印中文字符。 我想在控制台上看到unicode字符串

谢谢

1 个答案:

答案 0 :(得分:2)

'unicode-escape'编码可以显示代码点:

>>> s = u'統計情報'
>>> print(s.encode('unicode-escape'))
\u7d71\u8a08\u60c5\u5831

但是如果你想直接使用这些整数,最好应用ord

>>> ord(s[0])
32113
>>> 0x7d71
32113
>>> [hex(ord(c)) for c in s]
['0x7d71', '0x8a08', '0x60c5', '0x5831']

我在这里描述的内容适用于Python 2和Python 3。