加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – Mako模板中的UnicodeEncodeError

发布时间:2020-12-20 11:16:21 所属栏目:Python 来源:网络整理
导读:我有以下文件 dummy.py #!c:/Python27/python.exe -ufrom mako import exceptionsfrom mako.template import Templateprint "Content-type: text/html"print#VARIABLE = "WE" VARIABLE = "我们"template = Template(filename='../template/dummy.html',outpu
我有以下文件

dummy.py

#!c:/Python27/python.exe -u

from mako import exceptions
from mako.template import Template

print "Content-type: text/html"
print

#VARIABLE = "WE" 
VARIABLE = "我们"
template = Template(filename='../template/dummy.html',output_encoding='utf8')
try:
    print template.render(VARIABLE=VARIABLE)
except:
    print exceptions.html_error_template().render()

dummy.html(以UTF-8格式保存)

hello world
哈罗世界
${VARIABLE}

我已经审阅了http://www.makotemplates.org/docs/unicode.html的指令

但是,我仍然得到错误

UnicodeDecodeError: ‘ascii’ codec
can’t decode byte 0xe6 in position 0:
ordinal not in range(128)

我错过了什么?

解决方法

template = Template(filename='../template/dummy.html',default_filters=['decode.utf8'],input_encoding='utf-8',output_encoding='utf-8')

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读