This question already has an answer here:
- Python, Unicode, and the Windows console 13 answers
I am running Windows 7 and its console has been configured to use Consolas font, which gives me a possibility of Unicode output. The ability to read Unicode in console has been proved by me many times for programs such as Far Manager: both Cyrillics and German äöü letters can be read on the same console in the same string without encoding switching.
Now about Python.
I am trying very hard, but can't see Unicode in it's output.
By default print(sys.stdout.encoding)
prints cp866
and stdout is unable to output any characters except ASCII and Cyrillics.
It gives me following results:
print("Ля-ля äöüÄÖÜß")
UnicodeEncodeError: 'charmap' codec can't encode characters in position 6-12: character maps to <undefined>
print("Ля-ля äöüÄÖÜß".encode("utf-8"))
b'\xd0\x9b\xd1\x8f-\xd0\xbb\xd1\x8f \xc3\xa4\xc3\xb6\xc3\xbc\xc3\x84\xc3\x96\xc3\x9c\xc3\x9f'
Ok, I've set the PYTHONIOENCODING
environment variable in batch file:
SET PYTHONIOENCODING=UTF-8
and got:
print(sys.stdout.encoding)
UTF-8
print("Ля-ля äöüÄÖÜß")
╨Ы╤П-╨╗╤П ├д├╢├╝├Д├Ц├Ь├Я
print("Ля-ля äöüÄÖÜß".encode("utf-8"))`
b'\xd0\x9b\xd1\x8f-\xd0\xbb\xd1\x8f \xc3\xa4\xc3\xb6\xc3\xbc\xc3\x84\xc3\x96\xc3\x9c\xc3\x9f'
What to do?