Skip site navigation (1)Skip section navigation (2)
Date:      Fri, 8 Feb 2008 16:06:50 +0100
From:      "Heiko Wundram (Beenic)" <wundram@beenic.net>
To:        freebsd-questions@freebsd.org
Subject:   Re: How do I get unicode support in python?
Message-ID:  <200802081606.50274.wundram@beenic.net>
In-Reply-To: <582715960802080626m22c586a4j5c8de009294a0aae@mail.gmail.com>
References:  <582715960802080626m22c586a4j5c8de009294a0aae@mail.gmail.com>

next in thread | previous in thread | raw e-mail | index | archive | help
Am Freitag, 8. Februar 2008 15:26:48 schrieb Eric Mesa:
> I'm running a web server with FreeBSD 6.1-RELEASE and python 2.4.3.  I'm
> unable to print any characters outside of ascii.  I have tried this code =
on
> my Linux computer, which has python 2.5.x and it works - so the code is
> solid.
>
> What do I need to do to get python on the web server to have unicode
> support?  Is there a module/package I need to import in the 2.4 series?  =
Or
> is there some package/port I need to install?  Or do I just recompile
> python with some different flags?  (And does that entail any uninstalling
> first?)

=46or Python to be able to "print" unicode characters to the console, it mu=
st=20
know the encoding of the console. Generally, this entails setting up LC_ALL=
=20
and LANG and of course your terminal (emulator) appropriately, and testing=
=20
whether the interpreter sets the correct encoding on startup (which can be=
=20
found as sys.getdefaultencoding()). When the encoding that the interpreter=
=20
uses to "print" _unicode_-strings cannot encode the unicode characters you=
=20
hand it to the current default encoding, the codec barfs:

[modelnine@phoenix ~]$ python
Python 2.5.1 (r251:54863, Nov  6 2007, 19:02:51)
[GCC 4.2.1 20070719  [FreeBSD]] on freebsd7
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.getdefaultencoding()
'ascii'
>>> print u"\xfa"
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character u'\xfa' in positio=
n=20
0: ordinal not in range(128)
>>> print u"\xfa".encode("latin-1")
=FA
>>>

Basically, the easiest resolution is to do the conversion yourself (like I =
did=20
in the second example). The other possibility is to change the deault=20
encoding to something that matches your default console (probably latin-1),=
=20
which you can do in /usr/local/lib/python2x/site.py.

HTH!

=2D-=20
Heiko Wundram
Product & Application Development



Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?200802081606.50274.wundram>