[Tutor] A unicode print question

Peter Otten __peter__ at web.de
Fri Jul 30 21:48:12 CEST 2010


Joel Goldstick wrote:

> I was reading this: http://diveintopython.org/xml_processing/unicode.html
> 
> and tried the exercise:
> 
>>>> s = u'La Pe\xf1a'         [image: 1]
>>>> print s                   [image: 2]
> Traceback (innermost last):
>   File "<interactive input>", line 1, in ?
> UnicodeError: ASCII encoding error: ordinal not in range(128)
>>>> print s.encode('latin-1') [image: 3]
> La Peña
> 
>  [image:
>  
[1]<http://diveintopython.org/xml_processing/unicode.html#kgp.unicode.2.1>
> 
> But oddly enough, when I typed it into my python shell I did NOT get the
> UnicodeError, and I wonder why not:
> 
> Python 2.6.5 (r265:79063, Apr 16 2010, 13:09:56)
> [GCC 4.4.3] on linux2
> Type "help", "copyright", "credits" or "license" for more information.
>>>> s = u'La Pe\xf1a'
>>>> print s
> La Peña
>>>> import sys
>>>> sys.getdefaultencoding()
> 'ascii'
>>
> 
> 
> Maybe an earlier version of python produces the error, but not 2.6.5?

This works a bit differently, probably since Python 2.3. See

http://docs.python.org/library/stdtypes.html#file.encoding

The "old" behaviour may still bite you when you redirect stdout.

$ python -c"print u'Pe\xf1a'"
Peña
$ python -c"print u'Pe\xf1a'" > /dev/null
Traceback (most recent call last):
  File "<string>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xf1' in position 
2: ordinal not in range(128)

Peter



More information about the Tutor mailing list