Python 2.7: utf-8 output in windows console

Let's say

s = u"test\u0627\u0644\u0644\u0647 \u0623\u0643\u0628\u0631\u7206\u767A\u043E\u043B\u043E\u043B\u043E" 

If I try to print it directly,

 >>> print s Traceback (most recent call last): File "<stdin>", line 1, in <module> UnicodeEncodeError: 'cp932' codec can't encode character u'\u0627' in position 4: illegal multibyte sequence 

So, I am changing the console to UTF-8 from Python (otherwise it will not understand my input).

 import win32console win32console.SetConsoleOutputCP(65001) win32console.SetConsoleCP(65001) 

And then print the string encoded as utf-8, because Python does not know that chcp 65001 is UTF-8 (known bug ).

 >>> print s.encode('utf-8') testالله أكبر爆発Traceback (most recent call last): File "<stdin>", line 1, in <module> IOError: [Errno 0] Error 

As you can see, it prints successfully until it hits a new line, then it throws an IOError.

The following workaround works:

 def safe_print(str): try: print str.encode('utf-8') except: pass print >>> safe_print(s) testالله أكبر爆発 

But there must be a better way. Any suggestions?

+6
source share
2 answers

Finding SO for windows python utf8 brings as a first result the question Getting python to print in UTF8 on Windows XP with a console that describes that there is a problem with printing utf8 on Windows with Python.

+4
source

I have not tested it on windows, but here you can get a small script initialization for win / linux to set the output encoding including the logging interface, etc. Does the module also output in color (including updating the logging interface)? but you can easily cut off unnecessary functionality :-).

How to call an unpainted option:

 #!/usr/bin/env python # -*- coding: utf-8 -*- from setupcon import setup_console setup_console('utf-8', False) 

and color option:

 import setupcon setupcon.setup_console() import logging #... if setupcon.ansi: logging.getLogger().addHandler(setupcon.ColoredHandler()) 

If the solution works for you, you can read the documentation here: http://habrahabr.ru/blogs/python/117236/ , in Russian, or I / someone can translate this for you upon request :-).

+1
source

Source: https://habr.com/ru/post/895139/


All Articles