Unicode and print with python 2.1

Alex Martelli aleaxit at yahoo.com
Thu Jun 28 11:52:40 EDT 2001


"Alexandre Fayolle" <alf at leo.logilab.fr> wrote in message
news:slrn9jmbi4.cf2.alf at leo.logilab.fr...
> Hello,
>
> This is probably going to sound stupid, but here I go anyway. Please refer
> me to TFM if this is what I deserve.
>
> Is it normal to get UnicodeError when I try to print a unicode string with
> Python 2.1?

Alas, yes.  That's because *by default* the standard codec used
by Python is 'ascii', which only covers the 7-bit range that is
common to just about all encodings, and NOT the characters with
the 8th bit set that one needs for various "national character"
encodings.  I think it's reasonable for most Western European
Python sites to have a siteconfig.py module that sets the default
encoding to 'latin-1' (aka iso-8859-1, but don't use that name:
it uses, I believe, a slower implementation, while 'latin-1' has
a built-in implementation, as far as I understand things).

Note that you MUST do it in siteconfig.py (or by editing site.py,
but that's not advisable), because the setdefaultencoding()
function (originally in sys) is then removed by site.py, so the
default encoding cannot be changed "in midstream".  Well, you
COULD:
    import sys
    reload(sys)
    sys.setdefaultencoding('latin-1')
but that would be clearly against the design intent of the
Unicode subsystem!-)


Alex






More information about the Python-list mailing list