在Jinja2模板中使用utf-8字符

亚历克斯

我在使用Jinja2渲染模板时尝试使用utf-8字符。这是我的模板的样子:

<!DOCTYPE HTML>
<html manifest="" lang="en-US">
<head>
    <meta charset="UTF-8">
    <title>{{title}}</title>
...

title变量设置如下:

index_variables = {'title':''}
index_variables['title'] = myvar.encode("utf8")

template = env.get_template('index.html')
index_file = open(preview_root + "/" + "index.html", "w")

index_file.write(
    template.render(index_variables)
)
index_file.close()

现在,问题是myvar是从消息队列中读取的消息,并且可以包含那些特殊的utf8字符(例如“SéptimoCine”)。

呈现的模板如下所示:

...
    <title>S\u00e9ptimo Cine</title>
...

我希望它是:

...
    <title>Séptimo Cine</title>
...

我已经进行了几次测试,但是无法正常工作。

  • 我尝试设置没有.encode(“ utf8”)的title变量,但是会引发异常(ValueError:预期为字节对象,而不是unicode对象),所以我的猜测是初始消息是unicode

  • 我已经使用chardet.detect来获取消息的编码(为“ ascii”),然后执行以下操作:myvar.decode(“ ascii”)。encode(“ cp852”),但标题仍无法正确呈现。

  • 我还确保我的模板是UTF-8文件,但没有任何区别。

有关如何执行此操作的任何想法?

卢卡斯·格拉夫(Lukas Graf)

TL; DR:

  • 将Unicode传递template.render()
  • 将渲染的unicode结果编码为字节串,然后再将其写入文件

这让我有些困惑。因为你做

index_file.write(
    template.render(index_variables)
)

在一条语句中,基本上只涉及Python的一行,因此您得到的回溯具有误导性:重新创建测试用例时遇到的异常不是发生在中template.render(index_variables),而是发生了index_file.write()所以像这样拆分代码

output = template.render(index_variables)
index_file.write(output)

是诊断确切UnicodeEncodeError发生地点的第一步

Jinja返回unicode,但您让其渲染模板。因此,您需要先将结果编码为字节串,然后才能将其写入文件:

index_file.write(output.encode('utf-8'))

第二个错误是,你在通过utf-8编码字节串到template.render()-金贾要统一因此,假设您myvar包含UTF-8,则需要先将其解码为unicode:

index_variables['title'] = myvar.decode('utf-8')

因此,综上所述,这对我有用:

# -*- coding: utf-8 -*-

from jinja2 import Environment, PackageLoader
env = Environment(loader=PackageLoader('myproject', 'templates'))


# Make sure we start with an utf-8 encoded bytestring
myvar = 'Séptimo Cine'

index_variables = {'title':''}

# Decode the UTF-8 string to get unicode
index_variables['title'] = myvar.decode('utf-8')

template = env.get_template('index.html')

with open("index_file.html", "w") as index_file:
    output = template.render(index_variables)

    # jinja returns unicode - so `output` needs to be encoded to a bytestring
    # before writing it to a file
    index_file.write(output.encode('utf-8'))

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

如何在 jinja2 模板中使用占位符?

来自分类Dev

UTF-8字符的属性

来自分类Dev

如何在Jinja2的模板变量中使用特殊字符?

来自分类Dev

使用HTML / UTF-8字符作为光标

来自分类Dev

使用CListCtrl显示utf-8字符

来自分类Dev

使用CListCtrl显示utf-8字符

来自分类Dev

Perl DBI,MySQL中使用ő和ű字符的UTF8字符编码

来自分类Dev

Perl DBI,MySQL中使用ő和ű字符的UTF8字符编码

来自分类Dev

使用Knitr的Pandoc模板变量中的UTF-8字符

来自分类Dev

从包含utf 8字符的属性文件中读取

来自分类Dev

utf8字符集与Thymeleaf

来自分类Dev

无法转换UTF-8字符-Python

来自分类Dev

UTF8字符串上的==是否安全?

来自分类Dev

从字节流中读取utf-8字符

来自分类Dev

UTF 8字符的Base64解码并打印

来自分类Dev

单个UTF-8字符到字节

来自分类Dev

mysql regex utf-8字符

来自分类Dev

重写htaccess中的utf8字符

来自分类Dev

灯箱显示UTF-8字符而不是箭头

来自分类Dev

无法用RegExp替换UTF-8字符

来自分类Dev

UTF8字符编码错误

来自分类Dev

UTF-8字符集问题

来自分类Dev

R,utf-8字符似乎无法滑动

来自分类Dev

在python 2.7中打印UTF-8字符

来自分类Dev

Sweet Alerts显示UTF-8字符错误

来自分类Dev

imagestring()函数中的utf-8字符

来自分类Dev

Python反转UTF-8字符串

来自分类Dev

Netbeans C ++不打印UTF-8字符

来自分类Dev

Internet Explorer中的粗体UTF-8字符