python3 print unicode to windows xp console encode cp437

To print Unicode characters that can’t be represented using the console codepage, you could use win-unicode-console Python package that uses Unicode API such as ReadConsoleW/WriteConsoleW() to read/write Unicode from/to Windows console directly:

#!/usr/bin/env python3
import win_unicode_console

    print('Bla \u2013 großes')

save it to file, and run it:

C:\> py

You should see:

Bla – großes

As a preferred alternative, you could use run module (included in the package), to run an ordinary script with enabled Unicode support in Windows console:

C:\> py -m run

To install win_unicode_console module, run:

C:\> pip install win-unicode-console

Make sure to select a font able to display Unicode characters in Windows console.

To save the output of a Python script to a file, you could use PYTHONIOENCODING envvar:

C:\> set PYTHONIOENCODING=utf-8:backslashreplace
C:\> py >output_utf8.txt

Do not hardcode the character encoding of your environment inside your script, print Unicode instead. The examples show that the same script may be used to print to the console and to a file using different encodings and different methods.

Leave a Comment