How to display Chinese in a list retrieved from database via python

Gabriel Genellina gagsl-py2 at yahoo.com.ar
Sat Dec 27 03:08:29 EST 2008


En Sat, 27 Dec 2008 03:03:24 -0200, zxo102 <zxo102 at gmail.com> escribió:
> On 12月26日, 下午3时16分, "Mark Tolonen" <metolone+gm... at gmail.com>  
> wrote:
>>
>> I was able to display 中文 successfully with this code:
>>
>> f=open('test.html','wt')
>> f.write('''<html><head>
>> <META HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=gb2312">
>> <title>test</title></head>
>> <body>\xd6\xd0\xce\xc4</body></html>''')
>> f.close()
>>
> Mark,
>    I have exactly copied your code into the htdocs of my Apache
> server,
>
> <html><head>
> <META HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=gb2312">
> <title>test</title></head>
> <body>\xd6\xd0\xce\xc4</body></html>
>
> but it still shows me \xd6\xd0\xce\xc4. Any ideas?

That's not the same thing as Mark T. said.
The original was Python code to *write* a test file that you could open in
a browser. Things like "\xd6\xd0" are escape sequences interpreted by
Python, not meant to literally appear in a file. Like \n -- it means
"start a new line", one wants a new line in the output, *not* a backslash
and a letter n. "\xd6\xd0" generate *two* bytes, not eight. If the file is  
interpreted as containing latin-1 characters, you see them as ÖÐ. But due  
to the "charset=gb2312" line, those two bytes together make the ideograph  
中.

So, write the Python code (from f=open... up to f.close()) in a file and
execute it. Then open the generated test.html file. You should see the two
ideographs.

-- 
Gabriel Genellina




More information about the Python-list mailing list