Jinja2编码到rend

2024-10-01 00:14:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个python字符串:

<p> <a href="http://blog.alexmaccaw.com/how-to-travel-around-the-world-for-a-year">How to travel around the world for a year • Alex MacCaw<span class="small">blog.alexmaccaw.com</span> </a></p>

<type 'str'>

这个问题是由在我试图使用Jinja呈现它时引起的。在

^{pr2}$

最后出现这样的错误:

UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 125: ordinal not in range(128)

我试过各种编码和解码方法,但总有类似的错误。在

有什么想法吗?在


Tags: theto字符串incomforworld错误
2条回答

尝试使用htmll代码:

&middot;

希望这有帮助!在

对python2中的文本使用unicode,而不是str对象。尤其是对于非ASCII字符的文本。str有时只是巧合。在

(如果源文件中有非ASCII文本,则还需要指定源编码):

# coding: utf-8
mystring = u'<p> <a href="http://blog.alexmaccaw.com/how-to-travel-around-the-world-for-a-year">How to travel around the world for a year • Alex MacCaw<span class="small">blog.alexmaccaw.com</span> </a></p>'

(在Python代码中而不是在模板或数据库中使用HTML可能也是个坏主意…)

相关问题 更多 >