How to set default encoding for print?

18 views
Skip to first unread message

kj

unread,
Jan 31, 2010, 10:38:04 AM1/31/10
to

It gets tedious to have to append .encode('utf-8') to all my unicode
strings when I print them, as in:

print foobar.encode('utf-8')

I want to tell python to apply this encoding automatically to
anything argument passed to print.

How can I do this?

TIA!

K

PS: BTW, sys.setdefaultencoding is *not* the answer, since this
method gets removed by site.py...

Diez B. Roggisch

unread,
Jan 31, 2010, 10:54:23 AM1/31/10
to
Am 31.01.10 16:38, schrieb kj:

Just wrap sys.stdout in a codecs-module based file-wrapper.

import codecs
import sys

sys.stdout = codecs.getwriter("utf-8")(sys.stdout)

print u"ö"

Diez

kj

unread,
Jan 31, 2010, 11:28:55 AM1/31/10
to

>import codecs
>import sys

>print u"ö"

That's exactly what I was looking for. Thanks!

kynn

Reply all
Reply to author
Forward
0 new messages