469,592 Members | 1,713 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,592 developers. It's quick & easy.

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 3764
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 suresh191 | last post: by
4 posts views Thread by guiromero | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.