在HTML中显示不寻常的字符



我从数据库中摘下以下字符串:

"赖特突然被陷入了遥不可及的存在,他表现得很好……一旦他适应了它。"

这正是在控制台上打印的文本。请注意三个点是一个字符。

现在,当我通过模板将这些数据传递到HTML时,我会收到以下错误:

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

任何想法如何解决?我需要以某种方式对UTF-8进行编码。我正在使用Python作为后端。

编辑

后端代码是:

print "INFO: ", data[2]
return render_template('index.html', title=data[1], info=data[2].encode("utf-8"), backdrop=data[4], imdbrat=data[7], rtrat=data[9], cert=data[10], yt=data[11], runtime=data[12]);

和模板(index.html)我有:

<p> {{info}} </p>

输出为:

INFO:  The life of Danny Wright, a salesman forever on the road, veers into dangerous and surreal territory when he wanders into a Mexican bar and meets a mysterious stranger, Julian, who's very likely a hit man. Their meeting sets off a chain of events that will change their lives forever, as Wright is suddenly thrust into a far-from-mundane existence that he takes to surprisingly well … once he gets acclimated to it.
[2016-12-17 21:06:14,846] ERROR in app: Exception on / [GET]
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1988, in wsgi_app
    response = self.full_dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1641, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1544, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1639, in full_dispatch_request
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1625, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/dennis/moviechoosy/moviechoosy/app.py", line 31, in home
    return render_template('index.html', title=data[1], info=data[2].encode("utf-8"), backdrop=data[4], imdbrat=data[7], rtrat=data[9], cert=data[10], yt=data[11], runtime=data[12]);
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 383: ordinal not in range(128)

'...'字符是水平省略号。

>>> import unicodedata
>>> c = unicodedata.lookup('HORIZONTAL ELLIPSIS')
>>> c
u'u2026'
>>> print c
…

编码为UTF-8,以0xe2开头:

>>> b = c.encode('utf-8')
>>> b
'xe2x80xa6'

烧瓶文档说,将模板的非ASCII字符串传递给render_template时应解码为Unicode。

如果您在字符串中需要ASCII以外的其他任何东西,则必须对此进行标记 将其作为Unicode字符串的字符串通过将其前缀加上小写u。(喜欢 u'hänsel和gretel')

因此,您的数据库中似乎有一个python2字符串,该字符串编码为UTF-8。因此,您需要解码为unicode

>>> u = b.decode('utf-8')

然后烧瓶/jinja2应该正确处理。

最新更新