UTF-8 output problems

Michael B. Trausch fd0man at gmail.com
Sat Mar 10 02:11:26 CET 2007


I am having a slight problem with UTF-8 output with Python.  I have the
following program:

x = 0

while x < 0x4000:
    print u"This is Unicode code point %d (0x%x): %s" % (x, x,
unichr(x))
    x += 1

This program works perfectly when run directly:

mbt at pepper:~/tmp$ python test.py
This is Unicode code point 0 (0x0):
This is Unicode code point 1 (0x1):
This is Unicode code point 2 (0x2):
This is Unicode code point 3 (0x3):
This is Unicode code point 4 (0x4):
This is Unicode code point 5 (0x5):
This is Unicode code point 6 (0x6):
This is Unicode code point 7 (0x7):
This is Unicode code point 8 (0x8):
This is Unicode code point 9 (0x9):
This is Unicode code point 10 (0xa):
(... continued)

However, when I attempt to redirect the output to a file:

mbt at pepper:~/tmp$ python test.py >f
Traceback (most recent call last):
  File "test.py", line 6, in <module>
    print u"This is Unicode code point %d (0x%x): %s" % (x, x,
unichr(x))
UnicodeEncodeError: 'ascii' codec can't encode character u'\x80' in
position 39: ordinal not in range(128)

This is slightly confusing to me.  The output goes all the way to the
end of the program when it is not redirected.  Why is Python treating
the situation differently when the output is redirected?  This failure
occurs for all redirection, by the way: >, >>, 1>2, pipes, and so forth.

Any ideas?

    — Mike

--
Michael B. Trausch
                    fd0man at gmail.com
Phone: (404) 592-5746
                          Jabber IM:
                    fd0man at gmail.com
              fd0man at livejournal.com
Demand Freedom!  Use open and free protocols, standards, and software!
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20070309/112d3fa4/attachment.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 196 bytes
Desc: This is a digitally signed message part
URL: <http://mail.python.org/pipermail/python-list/attachments/20070309/112d3fa4/attachment.pgp>


More information about the Python-list mailing list