Set the STDOUT encoding in Python

Source: Internet
Author: User

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).

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.