6

我正在使用 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ɒnˈtæ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)

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

4

3 回答 3

13

到处使用unicode文字。

“Python 中的 Unicode,完全揭秘”

于 2011-02-25T17:11:15.237 回答
6

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

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

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

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

于 2011-02-25T17:29:53.650 回答
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
>>> 
于 2013-08-21T14:38:30.317 回答