如何使 Python Interactive Shell 打印西里尔符号?

How to make Python Interactive Shell print cyrillic symbols?

我在我的项目中使用 Pymorphy2 作为西里尔词法分析器。 但是当我尝试打印出单词列表时,我得到了这个:

>>> for t in terms:
...     p = morph.parse(t)
...     if 'VERB' in p[0].tag:
...             t = p[0].normal_form
...     elif 'NOUN' in p[0].tag:
...             t = p[0].lexeme[0][0]
... 
>>> terms
[u'\u041f\u0430\u0432\u0435\u043b', u'\u0445\u043e\u0434\u0438\u0442', u'\u0434\u043e\u043c\u043e\u0439']

如何在 python shell 中打印俄语字符?

您看到的是 unicode 字符串的 repr 表示,如果您遍历列表或索引并打印每个字符串,您将看到所需的输出。

In [4]: terms
Out[4]: 
[u'\u041f\u0430\u0432\u0435\u043b',
 u'\u0445\u043e\u0434\u0438\u0442',
 u'\u0434\u043e\u043c\u043e\u0439'] # repr

In [5]: print terms[0] # str 
Павел

In [6]: print terms[1]
ходит

如果您希望将它们全部打印出来并且看起来像一个列表,请使用 str.format 和 str.join:

terms = [u'\u041f\u0430\u0432\u0435\u043b',
 u'\u0445\u043e\u0434\u0438\u0442',
 u'\u0434\u043e\u043c\u043e\u0439']

print(u"[{}]".format(",".join(terms)))

输出:

[Павел,ходит,домой]