Python unicode terminal output -
please me, tried lot of technics, cant make python print utf-8 symbols on screen.
i need read utf-8 coded standard input stream, count characters , print occurrences on screen.
here code:
import re 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 not working. on screen like:
(u'\u043e', 90)
and these characters (u'\u043e') reason not displayed normal letters.
but if in console following:
>>> = u'\u043e' >>> print
everything fine ,
what doing wrong? please explain me or point me right link. have been searched more 3 hours , have no success in solving problem.
thank lot.
counter.most_common()
returns list of tuples, when printing data structures in python tuples , lists inner object has representation printed (whatever repr(x)
return).
to print characters using str()
instead of repr()
need iterate list , print them separately, example:
for char, count in counted_chars: print char, count
Comments
Post a Comment