print u"\u0432": why is this so hard? UnciodeEncodeError

David Eppstein eppstein at ics.uci.edu
Thu Apr 8 03:20:20 EDT 2004


In article <c52lu9$j8c$03$1 at news.t-online.com>,
 "Martin v. Lowis" <martin at v.loewis.de> wrote:

> > That seems to work reasonably well in Python 2.3 (but not 2.2!). But
> > then for some obscure reason if I redirect stdout in my shell it fails.
> >   $ LANG=en_US.UTF-8 python2.3 -c 'print u"\u0432"' > /dev/null
> > 
> > Why is that?
> 
> Python 2.3 discovers the encoding of your terminal, and will display
> Unicode characters if the terminal supports them. Python 2.2 did not do
> that, and the new feature is mainly useful in interactive mode.

Py2.3 sure doesn't discover the encoding of my terminal automatically:

hyperbolic ~: python
Python 2.3 (#1, Sep 13 2003, 00:49:11) 
[GCC 3.3 20030304 (Apple Computer, Inc. build 1495)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> print u"\u0432".encode('utf8')
2
>>> print u"\u0432"
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
UnicodeEncodeError: 'ascii' codec can't encode character '\u432' in 
position 0: ordinal not in range(128)

(that 2 was something else looking like an angular capital B before I 
copied and pasted it into my newsreader...and yes, utf8 is the correct 
encoding.)

-- 
David Eppstein                      http://www.ics.uci.edu/~eppstein/
Univ. of California, Irvine, School of Information & Computer Science



More information about the Python-list mailing list