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

时间:2015-05-26 09:31:51

标签: python shell unicode character-encoding cyrillic

我在项目中使用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中打印俄文字符?

1 个答案:

答案 0 :(得分:4)

您正在看到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)))

输出:

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