Can't print Chinese to HTTP

Lie Ryan lie.1296 at gmail.com
Mon Nov 30 19:32:27 CET 2009


On 12/1/2009 4:05 AM, Gnarlodious wrote:
> Thanks for the help, but it doesn't work. All I get is an error like:
>
> UnicodeEncodeError: 'ascii' codec can't encode character '\\u0107' in
> position 0: ordinal not in range(128)

The error says it all; you're trying to encode the chinese character 
using 'ascii' codec.

 > malformed header from script. Bad header=\xe6\x99\x89

Hmmm... strange. The \xe6\x99\x89 happens to coincide with UTF-8 
representation of 晉. Why is your content becoming a header?

> #!/usr/bin/python
do you know what python version, exactly, that gets called by this 
hashbang? You mentioned that you're using python 3, but I'm not sure 
that this hashbang will invoke python3 (unless Mac OSX has made a 
progress above other linux distros and made python 3 the default python).

> Strangely, if I run the script in Terminal it acts as expected.

I think I see it now. You're invoking python3 in the terminal; but your 
server invokes python 2. Python 2 uses byte-based string literal, while 
python 3 uses unicode-based string literal. When you try to ' 
晉\n'.encode("utf-8"), python 2 tried to decode the string using 'ascii' 
decoder, causing the exception.



More information about the Python-list mailing list