By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,941 Members | 1,789 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,941 IT Pros & Developers. It's quick & easy.

Setting default output encoding

P: n/a
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
Share this Question
Share on Google+
1 Reply


P: n/a
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.