Changing stdin / stdout encoding at runtime in Python 3

In Python 3, stdin and stdout are TextIOWrappers that are encoded and therefore spit out ordinary strings (not bytes).

I can change the encoding that is used with the PYTHONIOENCODING environment variable . Is there a way to change this in my script?

+4
source share
2 answers

In fact, TextIOWrapper returns bytes. It takes a Unicode string and returns a byte string in a specific encoding. To change sys.stdout to use a specific encoding in a script, here is an example:

 Python 3.2.3 (default, Apr 11 2012, 07:15:24) [MSC v.1500 32 bit (Intel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> print('\u5000') Traceback (most recent call last): File "<stdin>", line 1, in <module> File "C:\dev\python32\lib\encodings\cp437.py", line 19, in encode return codecs.charmap_encode(input,self.errors,encoding_map)[0] UnicodeEncodeError: 'charmap' codec can't encode character '\u5000' in position 0: character maps to <undefined>>>> import io >>> import io >>> import sys >>> sys.stdout = io.TextIOWrapper(sys.stdout.buffer,encoding='utf8') >>> print('\u5000') 倀 

(my terminal is not UTF-8)

sys.stdout.buffer refers to the original byte stream. You can also use the following to write to stdout in a specific encoding:

 sys.stdout.buffer.write('\u5000'.encode('utf8')) 
+5
source

I am sure this is not possible. He explicitly says in the documentation that " If this is set before starting the interpreter , it overrides the encoding used for stdin / stdout / stderr"

I also got an error while trying to change sys.__stdin__.encoding , saying:

 Traceback (most recent call last): File "<stdin>", line 1, in <module> AttributeError: readonly attribute 

EDIT: in python 2.x, it was possible to change the encoding of stdin / out / err from a script. In python 3.x, it seems that you need to use locale (or set the environment variable from the command line before running the script).

EDIT: it might be interesting to read for you http://comments.gmane.org/gmane.comp.python.ideas/15313

0
source

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


All Articles