Sometimes in the context of a process, locale is set to support only ASCII character sets (such as Lang=c). At this point Python will set the standard output and standard error encoding to ASCII, resulting in the output of the Chinese times wrong.
One solution is to set up a locale that supports UTF-8, but it needs to be set before the Python process starts. After startup, initialization is done, and setting locale does not reinitialize those objects.
Another way is to write bytes directly to the Sys.stdout.buffer place. Theoretically no problem at all, but write the program to be very tired ...
I went to find out how to gracefully get a new sys.stdout out. Python 3 I/O no longer uses the I/O function of the C standard library, but directly uses the interface provided by the OS. The package is in the IO module, with buffered, buffered, binary, text.
After studying the documentation, Sys.stdout is an IO. Textiowrapper, there is a buffer property, inside is an IO. BufferedWriter. We use it to build a new IO. Textiowrapper, specify the encoding as UTF-8:
Import Sys
Import IO
Def setup_io ():
Sys.stdout = sys.__stdout__ = io. Textiowrapper (
Sys.stdout.detach (), encoding= ' Utf-8 ', line_buffering=true)
Sys.stderr = sys.__stderr__ = io. Textiowrapper (
Sys.stderr.detach (), encoding= ' Utf-8 ', line_buffering=true)
In addition to the encoding can be set here, you can set error handling and buffering. So this technique can also be used to tolerate coding errors and to change the buffer of standard output (no need to add-U at startup).
In fact, this is still not thorough enough. Python is useful in many places to the default encoding. For example, when subprocess specifies Universal_newlines=true, Python automatically decodes the standard input, output, and error, but before Python 3.6, the code here cannot be manually specified. Also has the parameter the code, also cannot specify (but may pass the bytes past).