472,125 Members | 1,393 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,125 software developers and data experts.

Setting default output encoding

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.

--
Torsten Bronger, aquisgrana, europa vetus
Jabber ID: br*****@jabber.org
(See http://ime.webhop.org for ICQ, MSN, etc.)
Aug 6 '07 #1
1 4014
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
Aug 6 '07 #2

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by nicolas | last post: by
11 posts views Thread by Roger Leigh | last post: by
5 posts views Thread by Hardy Wang | last post: by
1 post views Thread by MaTianyi | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.