From owner-freebsd-questions@FreeBSD.ORG Fri Feb 8 15:05:25 2008 Return-Path: Delivered-To: freebsd-questions@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id 1A79716A46C for ; Fri, 8 Feb 2008 15:05:25 +0000 (UTC) (envelope-from wundram@beenic.net) Received: from mail.beenic.net (mail.beenic.net [83.246.72.40]) by mx1.freebsd.org (Postfix) with ESMTP id A21DA13C442 for ; Fri, 8 Feb 2008 15:05:24 +0000 (UTC) (envelope-from wundram@beenic.net) Received: from [192.168.1.32] (a89-182-0-241.net-htp.de [89.182.0.241]) (using TLSv1 with cipher DHE-RSA-AES256-SHA (256/256 bits)) (No client certificate requested) by mail.beenic.net (Postfix) with ESMTP id B7301A44529 for ; Fri, 8 Feb 2008 16:05:21 +0100 (CET) From: "Heiko Wundram (Beenic)" Organization: Beenic Networks GmbH To: freebsd-questions@freebsd.org Date: Fri, 8 Feb 2008 16:06:50 +0100 User-Agent: KMail/1.9.7 References: <582715960802080626m22c586a4j5c8de009294a0aae@mail.gmail.com> In-Reply-To: <582715960802080626m22c586a4j5c8de009294a0aae@mail.gmail.com> MIME-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: quoted-printable Content-Disposition: inline Message-Id: <200802081606.50274.wundram@beenic.net> Subject: Re: How do I get unicode support in python? X-BeenThere: freebsd-questions@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: User questions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Fri, 08 Feb 2008 15:05:25 -0000 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 "", line 1, in 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