Chinaunix

标题: python 中文编码问题一例 [打印本页]

作者: albertlee    时间: 2006-07-10 05:30
标题: python 中文编码问题一例
TurboGears中,数据库使用 gbk , 模板保存在数据库中,取出模板字符串,及内容,保存到文件中。
    @expose()
    def publish(self):
        pages = Page.selectBy(p_filename="test.html")
        for page in pages:
            template_def = codecs.encode(page.p_template, 'gbk')
            #template_def = page.p_template
            nameSpace = {u'title': u'Hello World Example',
                         u'contents': codecs.encode(page.p_name,'gbk')}
            t = Template(template_def, searchList = [nameSpace])
            #f_out = codecs.open(SAVE_PATH + page.p_filename, 'w', 'gbk')
            f_out = open(SAVE_PATH + page.p_filename, 'w')
            f_out.write(t.__str__())
            f_out.close()
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/4614/showart_138479.html




欢迎光临 Chinaunix (http://bbs.chinaunix.net/) Powered by Discuz! X3.2