How to set default encoding for print?

K

kj

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...
 
D

Diez B. Roggisch

Am 31.01.10 16:38, schrieb kj:
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...

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
 

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,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top