How to display unicode with the CGI module?

Marc 'BlackJack' Rintsch bj_666 at gmx.net
Sun Nov 25 01:57:51 EST 2007


On Sat, 24 Nov 2007 15:58:56 -0800, coldpizza wrote:

> The problem I am having is that I get an error while trying to display
> Unicode UTF-8 characters via a Python CGI script.
> 
> The error goes like this: "UnicodeEncodeError: 'ascii' codec can't
> encode character u'\u026a' in position 12: ordinal not in range(128)".

Unicode != UTF-8.  You are not trying to send an UTF-8 encoded byte string
but an *unicode string*.  That's not possible.  If unicode strings should
"leave" your program they must be encoded into byte strings.  If you don't
do this explicitly Python tries to encode as ASCII and fails if there's
anything non-ASCII in the string.  The `encode()` method is your friend.

Ciao,
	Marc 'BlackJack' Rintsch



More information about the Python-list mailing list