the official way of printing unicode strings
Ben Finney
bignose+hates-spam at benfinney.id.au
Sun Dec 14 16:55:44 EST 2008
Piotr Sobolewski <NIE_DZIALA at gazeta.pl> writes:
> in Python (contrary to Perl, for instance) there is one way to do
> common tasks.
More accurately: the ideal is that there should be only one *obvious*
way to do things. Other ways may also exist.
> Could somebody explain me what is the official python way of
> printing unicode strings?
Try these:
<URL:http://effbot.org/zone/unicode-objects.htm>
<URL:http://www.reportlab.com/i18n/python_unicode_tutorial.html>
<URL:http://www.amk.ca/python/howto/unicode>
If you want something more official, try the PEP that introduced
Unicode objects, PEP 100:
<URL:http://www.python.org/dev/peps/pep-0100/>.
> I tried to do this such way:
> s = u"Stanisław Lem"
> print u.encode('utf-8')
> This works, but is very cumbersome.
Nevertheless, that says everything that needs to be said: You've
defined a Unicode text object, and you've printed it specifying which
character encoding to use.
When dealing with text, the reality is that there is *always* an
encoding at the point where program objects must interface to or from
a device, such as a file, a keyboard, or a display. There is *no*
sensible default encoding, except for the increasingly-inadequate
7-bit ASCII.
<URL:http://www.joelonsoftware.com/articles/Unicode.html>
Since there is no sensible default, Python needs to be explicitly told
at some point which encoding to use.
> 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
How does it “break”? What behaviour did you expect, and what
behaviour did you get instead?
--
\ “I hope that after I die, people will say of me: ‘That guy sure |
`\ owed me a lot of money’.” —Jack Handey |
_o__) |
Ben Finney
More information about the Python-list
mailing list