Python, Unicode, and Windows Console

Asked 1 years ago, Updated 1 years ago, 65 views

I'm using Python 2.5. When attempting to output a Unicode string on the Windows console, the error UnicodeEncodeError: 'charmap' code can't code character.... occurs. I'm guessing it's an error caused by the Windows console not allowing Unicode characters, is there a way to solve this? Is there any way to make ? output instead of an error?

python unicode

2022-09-21 18:24

1 Answers

UnicodeEncodeError: error 'charmap' code can't code character....

This error indicates that the Unicode character you are trying to print cannot be expressed in the character encoding (chcp) manner of the console you are currently using. Code pages usually use 8-bit encoding such as cp437:

>>> u"\N{EURO SIGN}".encode('cp437')
Traceback (most recent call last):
...
UnicodeEncodeError: 'charmap' codec can't encode character '\u20ac' in position 0:
character maps to

I'm guessing it's an error caused by the Windows console not allowing Unicode characters, is there a way to solve this?

The Windows console accepts Unicode characters and can also output if a matching font exists. Use the WriteConsoleW() API. If you are using win-unicode-console package, you do not need to modify the script, nor should you.

T:\> py -mpip install win-unicode-console
T:\> py -mrun your_script.py

See What's the deal with Python 3.4, Unicode, different languages and Windows?.

Is there any way to make ? output instead of an error?

If you are satisfied with replacing an unencoded character with ?, you can set the PYTHONIOENCODING environment variable.

T:\> set PYTHONIOENCODING=:replace
T:\> python3 -c "print(u'[\N{EURO SIGN}]')"
[?]


2022-09-21 18:24

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.