Setting default output encoding

  • Thread starter Torsten Bronger
  • Start date
T

Torsten Bronger

Hallöchen!

I start a python script with subprocess from another Python script
with

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.
I tried

environment = os.environ
environment["LC_CTYPE"] = environment["LANG"] = "de_DE.UTF-8"
python = subprocess.Popen(["python", "myscript.py"],
env = environment,
stdout=subprocess.PIPE, stderr=subprocess.PIPE)

for setting it to UTF-8, however, it didn't work. Any idea on how
to achieve this?

Tschö,
Torsten.
 
?

=?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,074
Latest member
StanleyFra

Latest Threads

Top