"zxo102" <[email protected]> wrote in message news:[email protected]...
On 12月29日, 下午5时06分, "Mark Tolonen" <[email protected]> wrote:"zxo102" <[email protected]> wrote in messagenews:2560a6e0-c103-46d2-aa5a-8604de4d1...@b38g2000prf.googlegroups.com...
[snip]
That said, learn to use Unicode strings by trying the following program, butset the first line to the encoding *your editor* saves files in. You can use the actual Chinese characters instead of escape codes this way. The encoding used for the source code and the encoding used for the html filedon't have to match, but the charset declared in the file and the encodingused to write the file *do* have to match. # coding: utf8 import codecs mydict = {} mydict['JUNK'] = [u'中文',u'中文',u'中文'] def conv_list2str(value): return u'["' + u'","'.join(s for s in value) + u'"]' f_str = u'''<html><head> <META HTTP-EQUIV="Content-Type" CONTENT="text/html;charset=gb2312"> <title>test</title> <script language=javascript> var test = %s alert(test[0]) alert(test[1]) alert(test[2]) </script> </head> <body></body></html>''' s = conv_list2str(mydict['JUNK']) f=codecs.open('test04.html','wt',encoding='gb2312') f.write(f_str % s) f.close() -Mark P.S. Python 3.0 makes this easier for what you want to do, because the representation of a dictionary changes. You'll be able to skip the conv_list2str() function and all strings are Unicode by default.Thanks for your comments, Mark. I understand it now. The list(escape codes): ['\xd6\xd0\xce\xc4','\xd6\xd0\xce\xc4','\xd6\xd0\xce\xc4'] is from a postgresql database with "select" statement.I will postgresql database configurations and see if it is possible to return ['中文','中 文','中文'] directly with "select" statement. ouyang
The trick with working with Unicode is convert anything read into the program (from a file, database, etc.) to Unicode characters, manipulate it, then convert it back to a specific encoding when writing it back. So if postgresql is returning gb2312 data, use:
data.decode('gb2312') to get the Unicode equivalent:
'\xd6\xd0\xce\xc4'.decode('gb2312')
u'\u4e2d\u6587'
print '\xd6\xd0\xce\xc4'.decode('gb2312')
中文 Google for some Python Unicode tutorials. -Mark -- http://mail.python.org/mailman/listinfo/python-list
