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

zxo102 zxo102 at gmail.com
Thu Dec 25 04:27:03 EST 2008


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.
>
> >  Anybody knows how to solve this problem?
>
> Upgrading to Python 2.6 would probably be beneficial due to its better
> handling of Unicode.
> Also, posting some of the actual code you're using (to generate
> JavaScript, I guess?) would definitely help.
>
> Merry Christmas,
> Chris
>
> --
> Follow the path of the Iguana...http://rebertia.com

Hi Chris:

I have to use python2.4 since many other python packages have not been
updated to 2.6.
Here is my demo:
I create a table in postgresql:

     create table my_table (
            id   serial,
            name char(20),
            phone char(20)
     );

and insert two records into the table
(4, '中文', '1334995555555')
(5, '中文', '3434343434343')


I would like to generate a html page dynamically, here is the demo
script

############################################################
def do_search(a):
     # 调用ODBC模块
     import odbc
     # 通过ODBC中的"my_odbc"和相应的数据库建立连接
     cc = odbc.odbc('dsn=wisco')
     cursor1 = cc.cursor()
     # 将数据存入数据库中的"my_table"
     #cursor1.execute("select * from my_table where name = '%s' "%a)
     cursor1.execute("select * from my_table where name like '%%%s%%'
"%a)
     rr = cursor1.fetchall()
     # 显示用户查到的数据
     row01 = rr[0]
     row02 = rr[1]
     print row01, row02
     html_str = ''
     #print "Content-Type: text/html\n\n"
     html_str += "<html><head> <title> test </title></head><body> \n"
     html_str += "<script language=javascript>\n"
     html_str += " var row01 = %s\n"
     html_str += " var row02 = %s\n"
     html_str += "</script>\n"
     html_str += " </body></html>\n"
     html_str = html_str%(row01,row02)
     f = open('c:\\xbop_sinopec\\apache\\htdocs\\test01.html','w')
     f.write(html_str)
     f.close

do_search('中文')
#########################################################

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.

Thanks for your help and Merry Christmas to you too.

ouyang





More information about the Python-list mailing list