Mako 模板中的 UnicodeEncodeError
我有以下文件
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”编解码器 无法解码位置 0 处的字节 0xe6: 序数不在范围内(128)
我错过了什么吗?
I have the following files
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 (Saved in UTF-8 format)
hello world
哈罗世界
${VARIABLE}
I had refereed to the instruction from http://www.makotemplates.org/docs/unicode.html
However, I still get error
UnicodeDecodeError: 'ascii' codec
can't decode byte 0xe6 in position 0:
ordinal not in range(128)
Anything I had missed out?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
是的,因为您试图将其呈现为 ASCII,但这是行不通的。您需要说明要使用什么output_encoding:
并且请不要有裸露的例外。添加您希望捕获的异常。
Yes, because you are trying to render it to ASCII, which doesn't work. You need to say what output_encoding to use:
And please don't have bare excepts. Add what exceptions you expect to catch.