Setting default output encoding

"Martin v. Löwis" martin at v.loewis.de
Mon Aug 6 08:30:43 CEST 2007


>     python = subprocess.Popen(["python", "myscript.py"],
>                               stdout=subprocess.PIPE, stderr=subprocess.PIPE)
> 
> However, this child script has sys.stdout.encoding set to None,
> which means that Unicode strings cannot be printed without trouble.

The automatic setting of .encoding is only done when the output is a
terminal. If you write to a file, or a pipe, Python's guess as to what
the encoding of that stream is might be easily wrong.

If you want to be able to write Unicode strings to stdout always,
it's best to wrap stdout with the result of codecs.getwriter. If
you want to use the user's locale as the encoding, use
locale.getpreferredencoding().

Regards,
Martin



More information about the Python-list mailing list