Python Forum

Full Version: unicodeencodeerror 'charmap' codec can't encode characters in position
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
The whole error is:
"unicodeencodeerror 'charmap' codec can't encode characters in position 1-6: character maps to <undefined>"

Appears when I run my program in the win console.

When I run it through the PyCharm IDE it does not appear.

The characters it's refering to, are non Latin characters.

I tried with .encode('utf-8') and .decode('utf-8') and seems ok.

Is this the 'proper' way to handle this?
That is not the whole error, which has multiple lines.

Also please show a minimal running code example, that we can run to produce this error you see.
By default, Windows does not use 'utf-8', it uses 'code pages'.  Your Pycharm settings most likely tells it to use 'utf-8', whereas your command terminal defaults to a code page.  The code page is determined by the locale you chose at Windows installation.  These code pages are based on standards created prior to the currently accepted 'utf-8' (which is a subset of Unicode).  When Windows does use Unicode, it is utf-16, when you want to use utf-8, Windows has a (rather crappy, imho) code page of 65001.

A list of all Python supported code pages can be found at:
Python main directory \Lib\encodings

A good read is here : Unicode
You should post an example and python version is important.
Quote:I tried with .encode('utf-8') and .decode('utf-8') and seems ok.
From where?

Use a better shell cmder.
In cmd:
C:\>which python
'which' is not recognized as an internal or external command,
operable program or batch file.

C:\>python -c "import sys; print(sys.stdout.encoding)"
utf-8

C:\>python -c "print('Spicy jalapeño ? ? ? ? ')"
Spicy jalapeño ? ? ? ? 

C:\>python -c "print('? ? ? ? ?')"
? ? ? ? ?
The same in cmder:
C:\
λ which python
/c/python36/python

C:\
λ python -c "import sys; print(sys.stdout.encoding)"
utf-8

C:\
λ python -c "print('Spicy jalapeño ☂ ☃ ★ ☁')"
Spicy jalapeño ☂ ☃ ★ ☁

C:\
λ python -c "print('♚ ✈ ☹ ☠ ☝')"
♚ ✈ ☹ ☠ ☝