简体   繁体   English

如何在控制台中打印汉字?

[英]How can I print Chinese characters in the console?

I am a little confused when I want to print Chinese characters in Python console: 当我想在Python控制台中打印汉字时,我有些困惑:

For example, let's say I read some Chinese characters 尽管美国一些重要行业 in a list word_list . 例如,假设我在word_list列表中阅读了一些尽管美国一些重要行业某些尽管美国一些重要行业word_list If I print like this: 如果我这样打印:

for item in word_list:
    print item

It displays the Chinese characters as in the console. 它显示了中国字符 在控制台中。 However, if I directly print like this: 但是,如果我直接这样打印:

print word_list

I got: [u'\尽\管', u'\美\国', u'\一\些', u'\重\要', u'\行\业', ... 我得到了: [u'\尽\管', u'\美\国', u'\一\些', u'\重\要', u'\行\业', ...

So how can I just print the word_list and let it displays in Chinese characters? 那么,如何打印word_list并使其以汉字显示呢?

This should work: 这应该工作:

print ''.join(word_list)

Here join will concatenate each two consecutive elements from word_list with '' char(which is the Empty string), which is as putting all element in word_list together in one string. 在这里join将把word_list每两个连续元素与'' char(这是空字符串)连接起来,就像将word_list中的所有元素word_list放在一个字符串中一样。 In your case, the element of word_list are already in unicode format, so it was straight forward. 在您的情况下, word_list的元素已经是unicode格式,因此很简单。

>>> l = [u'\u5c3d\u7ba1', u'\u7f8e\u56fd', u'\u4e00\u4e9b', u'\u91cd\u8981', u'\u884c\u4e1a']
>>> 
>>> 
>>> print ''.join(l)
尽管美国一些重要行业
>>> print l
[u'\u5c3d\u7ba1', u'\u7f8e\u56fd', u'\u4e00\u4e9b', u'\u91cd\u8981', u'\u884c\u4e1a']
>>> for i in l:
    print i 
尽管
美国
一些
重要
行业

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM