unicode

Sander Steffann sander at steffann.nl
Sun Jul 1 06:47:17 EDT 2007


Hi,

"Erik Max Francis" <max at alcyone.com> wrote in message 
news:Qp6dndf6_Mcn3BrbnZ2dnUVZ_hynnZ2d at speakeasy.net...
> 7stud wrote:
>
>> Based on this example and the error:
>>
>> -----
>> u_str = u"abc\u9999"
>> print u_str
>>
>> UnicodeEncodeError: 'ascii' codec can't encode character u'\u9999' in
>> position 3: ordinal not in range(128)
>> ------
>>
>> it looks like when I try to display the string, the ascii decoder
>> parses each character in the string and fails when it can't convert a
>> numerical code that is higher than 127 to a character, i.e. the
>> character \u9999.
>
> If you try to print a Unicode string, then Python will attempt to first 
> encode it using the default encoding for that file.  Here, it's apparent 
> the default encoding is 'ascii', so it attempts to encode it into ASCII, 
> which it can't do, hence the exception.

If you want to change the default encoding of your stdout and stderr, you 
can do something like this:

import codecs, sys
sys.stdout = codecs.getwriter('utf-8')(sys.stdout)
sys.stderr = codecs.getwriter('utf-8')(sys.stderr)

After doing this, print u_str will work as expected (when using an utf-8 
terminal)

- Sander






More information about the Python-list mailing list