Unicode string output

M.-A. Lemburg mal at lemburg.com
Thu Jan 25 05:31:24 EST 2001


Boudewijn Rempt wrote:
> 
> 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.

Note that setting the default *after* having run site.py may
cause strange effects do the caching of the default encoded
value in the Unicode objects themselves -- there are no
checks applied as to whether the encoding used in these actually
is the current default encoding. This is why the API is only
available in site.py.

Why would you want to set the encoding on a per application
basis, BTW ?

-- 
Marc-Andre Lemburg
______________________________________________________________________
Company:                                        http://www.egenix.com/
Consulting:                                    http://www.lemburg.com/
Python Pages:                           http://www.lemburg.com/python/




More information about the Python-list mailing list