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

zxo102 zxo102 at gmail.com
Sat Dec 27 00:03:24 EST 2008


On 12月26日, 下午3时16分, "Mark Tolonen" <metolone+gm... at gmail.com> wrote:
> "zxo102" <zxo... at gmail.com> wrote in message
>
> news:979fdf6d-0500-47ba-87fd-0f0361ca3059 at p2g2000prf.googlegroups.com...
>
>
>
>
>
> > On 12月26日, 上午4时58分, "Gabriel Genellina" <gagsl-... at yahoo.com.ar>
> > wrote:
> >> En Thu, 25 Dec 2008 07:27:03 -0200, zxo102 <zxo... at gmail.com> escribió:
>
> >> > On 12月25日, 下午3时35分, "Chris Rebert" <c... at rebertia.com> wrote:
> >> >> On Wed, Dec 24, 2008 at 11:29 PM, zxo102 <zxo... at gmail.com> wrote:
> >> >> > Hi,
> >> >> >   I retrieve some info in Chinese from postgresql  and assign it to
> >> >> > a
> >> >> > variable 'info' defined in javascript of a html page:
> >> >> >   var info = ['\xd6\xd0\xce\xc4','\xd6\xd0\xce\xc4','\xd6\xd0\xce
> >> >> > \xc4']
> >> >> >  But I want it to be
> >> >> >   var info = ['中文','中文','中文']
> >> >> >  since in html pages (via javascript), the items in chinese out of
> >> >> > the
> >> >> > former :['\xd6\xd0\xce\xc4','\xd6\xd0\xce\xc4','\xd6\xd0\xce\xc4']
> >> >> > can
> >> >> > not be displayed correctly when it is inserted into a html page. If
> >> >> > the list is  var info = ['中文','中文','中文'] , then everything
> >> >> > works
> >> >> > fine.
>
> >> > The html code is as follows
>
> >> > <html><head> <title> test </title></head><body>
> >> > <script language=javascript>
> >> >  var row01 = (1, '\xd6\xd0\xce\xc4', '1334995555555')
> >> >  var row02 = (2, '\xd6\xd0\xce\xc4', '3434343434343')
> >> > </script>
> >> >  </body></html>
>
> >> > But the '中文' is '\xd6\xd0\xce\xc4'. When row01 and row02 is called
> >> > from somewhere,
> >> > '\xd6\xd0\xce\xc4' can not be displayed correctly as '中文' in html
> >> > environment.
>
> >> You forgot to specify the page encoding, gb2312 presumably. If adding the
> >> encoding does not help, I'd say the problem must reside on how you later
> >> use row01 and row02 (your html page does not those variables for
> >> anything). '中文' is the same as '\xd6\xd0\xce\xc4', and both javascript
> >> and Python share the same representation for strings (mostly) so this
> >> should not be an issue.
>
> >> My PC is unable to display those characters, but I get "true" from this:
>
> >> <html><head><META HTTP-EQUIV="Content-Type" CONTENT="text/html;
> >> charset='gb2312'"><title> test </title></head>
> >> <body><script
> >> language=javascript>alert('中文'=='\xd6\xd0\xce\xc4')</script></body></html>
>
> >> --
> >> Gabriel Genellina
>
> > I did that: <META HTTP-EQUIV="Content-Type" CONTENT="text/html;
> >> charset='gb2312'">, but it does not work. Alert('\xd6\xd0\xce\xc4')
> >> displays some "junks". I am thinking there may be some way to convert
> >> '\xd6\xd0\xce\xc4' to '中文' in the list with python before I generate
> >> the html page. As a result, when I open the html file with Vi, I can see
> >> '中文' directly instead of  '\xd6\xd0\xce\xc4'. That will solve my
> >> problem.
>
> > Any ideas?
>
> Use charset=gb2312 instead of charset='gb2312'(remove single quotes).
>
> 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- 隐藏被引用文字 -
>
> - 显示引用的文字 -

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?

ouyang



More information about the Python-list mailing list