[prev in list] [next in list] [prev in thread] [next in thread]
List: python-list
Subject: Re: Unicode string output
From: Boudewijn Rempt <boud () valdyas ! org>
Date: 2001-01-24 6:43:15
[Download RAW message or body]
Fredrik Lundh wrote:
> Alexander Kostyrkin wrote:
>> Surprisingly printing a unicode string that contains a Japanese kanji
>> character raises an exception
>> For example
>>
>> print u"\u55f4"
>> UnicodeError: ASCII encoding error: ordinal not in range(128)
>>
>> Is there any way to overcome the problem?
>
> If you don't specify what encoding to use on output, Python assumes
> you're an encoding-ignorant american programmer <wink>, and defaults
> to ASCII.
>
> To use any other encoding, use the encode method:
>
> s = ...
> print s.encode("iso-latin-1")
> print s.encode("ascii", "ignore")
>
> Also see the codecs modules:
> http://www.python.org/doc/current/lib/module-codecs.html
>
> Cheers /F
>
I came across this problem recently, too, and I didn't want to have
to remember to explicitly encode my text everywhere in the application.
But Python doesn't really support setting a default encoding for an
application, only for _all_ of Python on a machine, because
sys.setdefaultencoding is removed after starting Python.
The solution was to add the following file to site-packages:
sitecustomize.py
import sys
sys.setappdefaultencoding=sys.setdefaultencoding
And call
import sys
sys.setappdefaultencoding("utf-8")
first thing in the application.
Boudewijn Rempt | http://www.xs4all.nl/~bsarempt/python
--
http://mail.python.org/mailman/listinfo/python-list
[prev in list] [next in list] [prev in thread] [next in thread]
Configure |
About |
News |
Add a list |
Sponsored by KoreLogic