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?
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}]')"
[?]
618 Uncaught (inpromise) Error on Electron: An object could not be cloned
581 PHP ssh2_scp_send fails to send files as intended
572 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
914 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
© 2024 OneMinuteCode. All rights reserved.