Change "preferred locale encoding"

[Using Python 3.2]

If I do not pass the encoding argument to open , the file opens with locale.getpreferredencoding() . So, for example, on my Windows machine, anytime I use open('abc.txt') , it will be decoded using cp1252 .

I would like to switch all my input files to utf-8 . Obviously, I can add encoding = 'utf-8' to all open function calls. Or, better, encoding = MY_PROJECT_DEFAULT_ENCODING , where the constant is somewhere defined globally.

But I was wondering if there is a clean way to avoid editing all my open calls by changing the β€œstandard” encoding. Is this something I can change by changing the locale? Or by changing a parameter within the locale? I tried to follow the Python manual, but did not understand how this is supposed to be used.

Thanks!

+6
source share
1 answer

On Windows with Python 3.3+, run chcp 65001 in the console or batch file before running Python to change the locale encoding to UTF-8.

+3
source

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


All Articles