我试图让 Mako 用 unicode 字符渲染一些字符串:
tempLook=TemplateLookup(..., default_filters=[], input_encoding='utf8',output_encoding='utf-8', encoding_errors='replace')
...
print sys.stdout.encoding
uname=cherrypy.session['userName']
print uname
kwargs['_toshow']=uname
...
return tempLook.get_template(page).render(**kwargs)
相关模板文件:
...${_toshow}...
输出是:
UTF-8
Deşghfkskhü
...
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc5 in position 1: ordinal not in range(128)
我认为字符串本身没有任何问题,因为我可以很好地打印它。
尽管我已经(很多)玩过input/output_encoding
和default_filters
参数,但它总是抱怨无法使用 ascii 编解码器进行解码/编码。
所以我决定尝试在文档中找到的示例,以下是“最好的”:
input_encoding='utf-8', output_encoding='utf-8'
#(note : it still raised an error without output_encoding, despite tutorial not implying it)
和
${u"voix m’a réveillé."}
结果是
voix mâ�a réveillé
我根本不明白为什么这不起作用。“魔术编码注释”也不起作用。所有文件都使用 UTF-8 编码。
我花了几个小时无济于事,我错过了什么吗?
更新 :
我现在有一个更简单的问题:
现在所有变量都是 unicode,我怎样才能让 Mako 在不应用任何东西的情况下呈现 unicode 字符串?传递一个空白过滤器/ render_unicode() 没有帮助。