2014-01-23 107 views
0

我有一个Python字符串:这个问题是由造成Jinja2的编码来渲染

<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'>

,当我尝试使用神社来呈现它。

renderedPage = template.render(starred = the_string_i_mentioned_higher_up) 

而与此错误结束:

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

我已经尝试了所有种类的编码和解码,但我总是得到一个类似的错误。

有什么想法?

+1

你怎么'the_string_i_mentioned_higher_up'或者它是如何编码的?你宣布它的字面? –

回答

1

尝试使用htmml代码:

&middot; 

希望这有助于!

+0

这是做了,感谢的正确方法。 – WhyNotHugo

0

使用unicode而不是str Python 2中的文本对象。特别是对于带有非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>' 

(这可能也是一个坏主意,有HTML在你的Python代码,而不是在模板或数据库...)