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

zxo102 zxo102 at gmail.com
Mon Dec 29 00:08:12 EST 2008


On 12月27日, 下午4时08分, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
wrote:
> En Sat, 27 Dec 2008 03:03:24 -0200,zxo102<zxo... 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

Thanks for your explanation. The example works now. It is close to my
real case.

I have a list in a dictionary and want to insert it into the html
file. I test it with following scripts of CASE 1, CASE 2 and CASE 3. I
can see "中文" in CASE 1 but that is not what I want. CASE 2 does not
show me correct things.
So, in CASE 3, I hacked the script of CASE 2 with a function:
conv_list2str() to 'convert' the list into a string. CASE 3 can show
me "中文". I don't know what is wrong with CASE 2 and what is right with
CASE 3.

Without knowing why, I have just hard coded my python application
following CASE 3 for displaying Chinese characters from a list in a
dictionary in my web application.

Any ideas?

Happy a New Year: 2009

ouyang



CASE 1:
########################################################
f=open('test.html','wt')
f.write('''<html><head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=gb2312">
<title>test</title>
<script language=javascript>
var test = ['\xd6\xd0\xce\xc4', '\xd6\xd0\xce\xc4', '\xd6\xd0\xce
\xc4']
alert(test[0])
alert(test[1])
alert(test[2])
</script>
</head>
<body></body></html>''')
f.close()

CASE 2:
#######################################################
mydict = {}
mydict['JUNK'] = ['\xd6\xd0\xce\xc4','\xd6\xd0\xce\xc4','\xd6\xd0\xce
\xc4']
f_str = '''<html><head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=gb2312">
<title>test</title>
<script language=javascript>
var test = %(JUNK)s
alert(test[0])
alert(test[1])
alert(test[2])
</script>
</head>
<body></body></html>'''

f_str = f_str%mydict
f=open('test02.html','wt')
f.write(f_str)
f.close()

CASE 3:
###################################################
mydict = {}
mydict['JUNK'] = ['\xd6\xd0\xce\xc4','\xd6\xd0\xce\xc4','\xd6\xd0\xce
\xc4']

f_str = '''<html><head>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=gb2312">
<title>test</title>
<script language=javascript>
var test = %(JUNK)s
alert(test[0])
alert(test[1])
alert(test[2])
</script>
</head>
<body></body></html>'''

import string

def conv_list2str(value):
   list_len = len(value)
   list_str = "["
   for ii in range(list_len):
       list_str += '"'+string.strip(str(value[ii])) + '"'
       if ii != list_len-1:
   	     list_str += ","
   list_str += "]"
   return list_str

mydict['JUNK'] = conv_list2str(mydict['JUNK'])

f_str = f_str%mydict
f=open('test03.html','wt')
f.write(f_str)
f.close()



More information about the Python-list mailing list