In my experience, this is a bit of a trial and error setting LC_ALL to something containing "UTF-8" worked for me once. Set environment variables: on Linux, Python's encoding defaults depend on this (at least partially).These are a few options to address this problem: In your case, the second condition isn't met for the Linux docker you mention: the encoding used is ASCII, which only supports characters found on an old English typewriter. the codec used covers a range of characters that is sufficient for your needs (such as UTF-8 or UTF-16, which contain all characters). the codec which Python uses is compatible with the one which the terminal expects, so the characters are displayed correctly (otherwise you get mojibake like "é" instead of "é").UTF-8 or ASCII) depends on the environment. The codec which is used for encoding (eg. Whenever you use the print() function, Python converts its arguments to a str and subsequently encodes the result to bytes, which are sent to the terminal (or whatever Python is run in). If you look closely, the raised exception is a Unicode EncodeError, not a - DecodeError. The problem is with the print() expression, not with the decode() method. Please read the warnings about this hack in the related answer and comments. # sys.setdefaultencoding() does not exist, here! Set your default encoding of python source files via environment variable: export PYTHONIOENCODING=utf8Īlso, in your source files you can state the encoding you prefer to be used explicitly, so it should work irrespective of environment setting (see this question + answer, python docs and PEP 263: #!/usr/bin/env python3Ĭoncerning the interpretation of encoding of files read by python, you can specify it explicitly in the open command with open(fname, "rt", encoding="utf-8") as f:Īnd there's a more hackish way with some side effects, but saves you to explicitly specify it each time import sys Thus the recommended way seems to be to tell your python instance to use utf-8 as default encoding: Adopted from this posting and also this blog: Seems ubuntu - depending on version - uses one encoding or another as default, and it may vary between shell and python as well.
0 Comments
Leave a Reply. |
Details
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |