【发布时间】:2014-02-05 08:52:02
【问题描述】:
我得到'ascii' codec can't decode byte 0xe2 in position 620: ordinal not in range(128)
再次在我的 appengine 的日志文件中,我不知道该怎么做。有问题的代码似乎是'name': form.name.data.decode('utf-8'),我已经尝试了一切让它工作、编码、解码和 unicode(),但不知道为什么它不是内置的。我通常在这里得到答案,我必须知道输入是什么,但为什么?当我尝试使用一些像 åäö 这样的外国字符时,它确实有效,所以问题出在哪里并不明显。完整的堆栈跟踪是
'ascii' codec can't decode byte 0xe2 in position 620: ordinal not in range(128)
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1505, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1077, in __call__
return handler.dispatch()
File "/base/data/home/apps/s~montaoproject/2015p.373396352072543150/authhandlers.py", line 34, in dispatch
webapp2.RequestHandler.dispatch(self)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 547, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~montaoproject/2015p.373396352072543150/main.py", line 2610, in post
'name': form.name.data.decode('utf-8')
File "/base/data/home/apps/s~montaoproject/2015p.373396352072543150/authhandlers.py", line 104, in render
self.response.write(self.jinja2.render_template(template_name, **values))
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2_extras/jinja2.py", line 158, in render_template
return self.environment.get_template(_filename).render(**context)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/jinja2-2.6/jinja2/environment.py", line 894, in render
return self.environment.handle_exception(exc_info, True)
File "templates/2.html", line 1680, in top-level template code
<div class="table_data"> {{form.text(cols="45", rows="10", placeholder=_('Please describe the product or service in a few simple sentences'))|safe}}{% if form.text.errors %} <div class="maintext">
File "/base/data/home/apps/s~montaoproject/2015p.373396352072543150/wtforms/fields/core.py", line 139, in __call__
return self.widget(self, **kwargs)
File "/base/data/home/apps/s~montaoproject/2015p.373396352072543150/main.py", line 2034, in __call__
return super(MyTextArea, self).__call__(field, **kwargs)
File "/base/data/home/apps/s~montaoproject/2015p.373396352072543150/wtforms/widgets/core.py", line 221, in __call__
return HTMLString('<textarea %s>%s</textarea>' % (html_params(name=field.name, **kwargs), escape(text_type(field._value()))))
File "/base/data/home/apps/s~montaoproject/2015p.373396352072543150/wtforms/fields/core.py", line 496, in _value
return text_type(self.data) if self.data is not None else ''
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 620: ordinal not in range(128)
更新 140209 06:02 CET
我也使用decode 得到错误
'ascii' codec can't decode byte 0xd7 in position 0: ordinal not in range(128)
Traceback (most recent call last):
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1511, in __call__
rv = self.handle_exception(request, response, e)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1505, in __call__
rv = self.router.dispatch(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1253, in default_dispatcher
return route.handler_adapter(request, response)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 1077, in __call__
return handler.dispatch()
File "/base/data/home/apps/s~montaoproject/2015r.373623653798528527/authhandlers.py", line 34, in dispatch
webapp2.RequestHandler.dispatch(self)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 547, in dispatch
return self.handle_exception(e, self.app.debug)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2.py", line 545, in dispatch
return method(*args, **kwargs)
File "/base/data/home/apps/s~montaoproject/2015r.373623653798528527/main.py", line 2664, in post
'name': form.name.data.encode('utf-8')
File "/base/data/home/apps/s~montaoproject/2015r.373623653798528527/authhandlers.py", line 104, in render
self.response.write(self.jinja2.render_template(template_name, **values))
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/webapp2-2.3/webapp2_extras/jinja2.py", line 158, in render_template
return self.environment.get_template(_filename).render(**context)
File "/base/data/home/runtimes/python27/python27_lib/versions/third_party/jinja2-2.6/jinja2/environment.py", line 894, in render
return self.environment.handle_exception(exc_info, True)
File "templates/2.html", line 223, in top-level template code
{{ form.phonenumber(size="17", maxlength="14")|safe }}<label for="phone_hidden">{{ form.phoneview|safe }}{% trans %}View on site{% endtrans %}</label><br />{% trans %}Use areacode{% endtrans %}<br>
File "/base/data/home/apps/s~montaoproject/2015r.373623653798528527/wtforms/fields/core.py", line 139, in __call__
return self.widget(self, **kwargs)
File "/base/data/home/apps/s~montaoproject/2015r.373623653798528527/wtforms/widgets/core.py", line 123, in __call__
kwargs['value'] = field._value()
File "/base/data/home/apps/s~montaoproject/2015r.373623653798528527/wtforms/fields/core.py", line 496, in _value
return text_type(self.data) if self.data is not None else ''
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd7 in position 0: ordinal not in range(128)
【问题讨论】:
-
form.name.data很可能已经是 Unicode,在这种情况下,decode会尝试首先将其编码为 ASCII,然后将 ASCII 解码为 UTF-8。 -
我觉得 Nick Johnson 的这篇文章真的很值得一读——blog.notdot.net/2010/07/Getting-unicode-right-in-Python
标签: python string google-app-engine python-2.7 jinja2