How to display unicode with the CGI module?

paul paul at subsignal.org
Sun Nov 25 08:06:04 EST 2007


Marc 'BlackJack' Rintsch schrieb:
> 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*.
Just to expand on this... It helps thinking of "unicode objects" and 
"strings" as seperate types (which they are). So there is no such thing 
like "unicode string" and you always need to think about when to 
encode() your unicode objects. However, this will change in py3k..., 
what's the new rule of thumb?

cheers
  Paul




More information about the Python-list mailing list