简体   繁体   中英

Python unicode terminal output

Please help me, I tried a lot of technics, but I cant make python print utf-8 symbols on the screen.

I need simply to read from a utf-8 coded standard input stream, count characters and print their occurrences on the screen.

Here is my code:

import re
from collections import Counter
import sys
import codecs

sys.stdin = codecs.getreader('utf-8')(sys.stdin)
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
chars = re.findall(r'.', sys.stdin.read().lower())
counted_chars = Counter(chars).most_common(20)
print counted_chars

I tried this

reload(sys)
sys.setdefaultencoding('utf-8')

But this was not working. I constantly get on the screen something like:

(u'\u043e', 90)

and these characters (u'\о') for some reason are not displayed as a normal letters.

But if I in console do following:

>>> a = u'\u043e'
>>> print a 

everything is fine and I get

What am I doing wrong? Please explain me or point me to right link. I have been searched for more then three hours and have no success in solving my problem.

Thank you a lot.

Counter.most_common() returns a list of tuples, when printing data structures in Python like tuples and lists any inner object has its representation printed (whatever repr(x) would return).

To print the characters using str() instead of repr() you will need to iterate this list and print them separately, for example:

for char, count in counted_chars:
    print char, count

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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