the official way of printing unicode strings

Piotr Sobolewski NIE_DZIALA at gazeta.pl
Sun Dec 14 00:48:19 EST 2008


Hello,

in Python (contrary to Perl, for instance) there is one way to do common
tasks. Could somebody explain me what is the official python way of
printing unicode strings?

I tried to do this such way:
s = u"Stanisław Lem"
print u.encode('utf-8')
This works, but is very cumbersome.

Then I tried to do this that way:
s = u"Stanisław Lem"
print u
This breaks when I redirect the output of my program to some file, like
that:
$ example.py > log

Then I tried to do this that way:
sys.stdout = codecs.getwriter("utf-8")(sys.__stdout__)
s = u"Stanisław Lem"
print u
This works but is even more combersome.

So, my question is: what is the official, recommended Python way?





More information about the Python-list mailing list