在Jinja的nl2br过滤器中避免Python UnicodeDecodeError

时间:2011-02-25 17:09:31

标签: python unicode jinja2 flask

我正在使用Jinja2的nl2br过滤器,它看起来像:

import re
from jinja2 import environmentfilter, Markup, escape

_paragraph_re = re.compile(r'(?:\r\n|\r|\n){2,}')

@evalcontextfilter
def nl2br(eval_ctx, value):
    result = u'\n\n'.join(u'<p>%s</p>' % p.replace('\n', '<br>\n')
                      for p in _paragraph_re.split(escape(value)))
    if eval_ctx.autoescape:
        result = Markup(result)
    return result

问题是“值”是否包含ascii字符(例如:“/mɒntænə/”导致它失败)。我收到这个错误:

Traceback (most recent call last):
  File "/usr/local/lib/python2.6/dist-packages/Flask-0.6.1-py2.6.egg/flask/app.py", line 889, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python2.6/dist-packages/Flask-0.6.1-py2.6.egg/flask/app.py", line 879, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/usr/local/lib/python2.6/dist-packages/Flask-0.6.1-py2.6.egg/flask/app.py", line 876, in wsgi_app
    rv = self.dispatch_request()
  File "/usr/local/lib/python2.6/dist-packages/Flask-0.6.1-py2.6.egg/flask/app.py", line 695, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/mcrittenden/Dropbox/Code/dropdo/dropdo.py", line 105, in view
    return render_template(template, src = url, data = content)
  File "/usr/local/lib/python2.6/dist-packages/Flask-0.6.1-py2.6.egg/flask/templating.py", line 85, in render_template
    context, ctx.app)
  File "/usr/local/lib/python2.6/dist-packages/Flask-0.6.1-py2.6.egg/flask/templating.py", line 69, in _render
    rv = template.render(context)
  File "/usr/local/lib/python2.6/dist-packages/Jinja2-2.5.5-py2.6.egg/jinja2/environment.py", line 891, in render
    return self.environment.handle_exception(exc_info, True)
  File "/home/mcrittenden/Dropbox/Code/dropdo/templates/text.html", line 1, in top-level template code
    {% extends "layout.html" %}
  File "/home/mcrittenden/Dropbox/Code/dropdo/templates/layout.html", line 25, in top-level template code
    {% block content %}{% endblock %}
  File "/home/mcrittenden/Dropbox/Code/dropdo/templates/text.html", line 8, in block "content"
    {{ data|nl2br }}
  File "/home/mcrittenden/Dropbox/Code/dropdo/dropdo.py", line 26, in nl2br
    for p in _paragraph_re.split(escape(value)))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc9 in position 12: ordinal not in range(128)

什么是最好的防止错误但不能完全删除问题字符?

3 个答案:

答案 0 :(得分:13)

在任何地方使用unicode文字。

"Unicode in Python, Completely Demystified"

答案 1 :(得分:6)

如果“value”除了ascii字符之外什么都没有,你希望它在整个应用程序中都是Unicode,而不是Unicode,除了你明确编码或解码它的几个地方。也将Unicode传递给您的模板。

如果你以某种方式获得字符串“/mɒntænə/”,你可能知道它的编码;用它: value = "/mɒnˈtænə/".decode(the_encoding)

你如何学习编码? HTTP请求知道其编码。 XML文件知道其编码。纯文本文件通常不会;你必须通过其他方式知道它的编码。

请注意, UTF-8不是Unicode ,尽管它是一种可以完全代表Unicode的编码。它仍然是一种编码,要从中获取Python Unicode字符串,您需要.decode("utf-8")它。

答案 2 :(得分:0)

尝试http://pypi.python.org/pypi/Unidecode

中的unidecode
>>> from unidecode  import unidecode
>>> m=u'My fianc\xe9 David'; print m; print unidecode(m)
My fiancé David
My fiance David
>>>