Pages

Wednesday, 6 January 2010

Python and UTF-8 on OSX: UnicodeEncodeError


an acute problem



UnicodeEncodeError:
'ascii' codec can't encode character u'\xe9'



I'm on a Mac Air with the latest Snow Leopard (10.6.2). I'm using Python 2.6.4 with unicode strings. I can't print appliqu� !?

I tried adding

# -*- coding: utf-8 -*-

at the head of my Python script, but I still get this complaint.

Solution


The encoding used for standard input, output, and standard error can be specified by setting the PYTHONIOENCODING environment variable before running the interpreter.

The value should be a string in the form <encoding> or <encoding>:<errorhandler>. The encoding part specifies the encoding�s name, e.g. utf-8 or latin-1; the optional errorhandler part specifies what to do with characters that can�t be handled by the encoding, and should be one of �error�, �ignore�, or �replace�.

export PYTHONIOENCODING=utf-8

does the trick.

� or you could just add this setting to your environment file: ~/.MacOSX/environment.plist

No comments:

Post a Comment