Z
Z
zencd2018-02-13 14:56:46
Python
zencd, 2018-02-13 14:56:46

Python 3 + Flask: charmap codec can't encode character?

A remote client has a Flask web server running, and when trying to print unicode to stdout, the following error occurs - it crashes on the 'Ł' character and the like:

File "C:\project\utils\mycode.py", line 260, in myfunk
    print("- [%s]" % text)
  File "C:\Python\Python36-32\lib\encodings\cp1251.py", line 19, in encode
    return codecs.charmap_encode(input,self.errors,encoding_table)[0]
UnicodeEncodeError: 'charmap' codec can't encode character '\xe4' in position 32:
   character maps to <undefined>

Everything would be fine, but I can’t reproduce the error on a similar configuration.
Please help me to overcome the problem.
The server is launched through such a batch, in a separate window "cmd.exe":
chcp 65001
set FLASK_DEBUG=1
set FLASK_APP=backend.py
venv\scripts\flask.exe run --port 5000

At the same time, which is strange, the simplest check on the client is successful:
from pprint import pprint
import sys
print(sys.version)
pprint('\u0141') # char 'Ł'

Conclusion:
3.6.4 (v3.6.4:d48eceb, Dec 19 2017, 06:04:45) [MSC v.1900 32 bit (Intel)]
'Ł'

Perhaps this is due to the fact that the flask-server is launched by its own executable.
Windows 7, Python 3.6

Answer the question

In order to leave comments, you need to log in

1 answer(s)
Z
zencd, 2018-02-16
@zencd

I forgot about one more magic... Here it works if added to the bat file:
set PYTHONIOENCODING=UTF-8

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question