Changing the "preferred language encoding" in Python 3 on Windows

I am using Python 3 (recently switched from Python 2). My code usually works on Linux, but sometimes (not often) on Windows. According to the Python 3 documentation for open() , the default encoding for a text file is from locale.getpreferredencoding() , unless the encoding argument is supplied. I want this default value to be utf-8 for my project, no matter what OS it is running on (currently it is always UTF-8 for Linux, but not for Windows). There are many open() calls in the project, and I don't want to add encoding='utf-8' to all of them. Thus, I want to change my preferred language encoding on Windows, as Python 3 sees it.

I found the previous question, β€œ Changing theβ€œ preferred language encoding ” , in which the accepted answer was accepted, so I thought I was fine. But, unfortunately, none of the proposed commands in this answer and the first comment work for me on Windows. in particular, this accepted answer and its first comment suggest running chcp 65001 and set PYTHONIOENCODING=UTF-8 , and I tried set PYTHONIOENCODING=UTF-8 see the transcript below from my cmd window:

 > py -i Python 3.4.3 ... >>> f = open('foo.txt', 'w') >>> f.encoding 'cp1252' >>> exit() > chcp 65001 Active code page: 65001 > py -i Python 3.4.3 ... >>> f = open('foo.txt', 'w') >>> f.encoding 'cp1252' >>> exit() > set PYTHONIOENCODING=UTF-8 > py -i Python 3.4.3 ... >>> f = open('foo.txt', 'w') >>> f.encoding 'cp1252' >>> exit() 

Note that even after both of the proposed commands, my open file encoding is still cp1252 instead of the supposed utf-8 .

+6
source share
2 answers

As in python3.5.1, this hack looks like this:

 import _locale _locale._getdefaultlocale = (lambda *args: ['en_US', 'utf8']) 

All files opened after this will read the default encoding utf8 .

+4
source

I know his real hacky workaround, but you can override the locale.getpreferredencoding() function as follows:

 import locale def getpreferredencoding(do_setlocale = True): return "utf-8" locale.getpreferredencoding = getpreferredencoding 

if you run it at an early stage, all files opened after (during my testing on win xp machine) open in utf-8, and since this overrides the module method, this applies to all platforms.

+3
source

Source: https://habr.com/ru/post/920637/


All Articles