如何处理我的UnicodeDecodeError?

2024-10-01 07:36:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我的数据存储包含值,我希望我的表单能够更新这些值。我在jinja2中使用wtforms和googleappengine。我收到一条我无法理解的错误消息:

'ascii' codec can't decode byte 0xc3 in position 10: ordinal not in range(128)
Traceback (most recent call last):
  File "/base/data/home/apps/s~bnano-www/wtform.356204665143172839/webapp2.py", line 1511, in __call__
    rv = self.handle_exception(request, response, e)
  File "/base/data/home/apps/s~bnano-www/wtform.356204665143172839/webapp2.py", line 1505, in __call__
    rv = self.router.dispatch(request, response)
  File "/base/data/home/apps/s~bnano-www/wtform.356204665143172839/webapp2.py", line 1253, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/base/data/home/apps/s~bnano-www/wtform.356204665143172839/webapp2.py", line 1077, in __call__
    return handler.dispatch()
  File "/base/data/home/apps/s~bnano-www/wtform.356204665143172839/handler.py", line 152, in dispatch
    response = super(NewBaseHandler, self).dispatch()
  File "/base/data/home/apps/s~bnano-www/wtform.356204665143172839/webapp2.py", line 547, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/base/data/home/apps/s~bnano-www/wtform.356204665143172839/webapp2.py", line 545, in dispatch
    return method(*args, **kwargs)
  File "/base/data/home/apps/s~bnano-www/wtform.356204665143172839/handler.py", line 101, in check_login
    return handler(self, *args, **kwargs)
  File "/base/data/home/apps/s~bnano-www/wtform.356204665143172839/handler.py", line 881, in get
    self.render_jinja('details.html', **data)
  File "/base/data/home/apps/s~bnano-www/wtform.356204665143172839/handler.py", line 119, in render_jinja
    **template_args))
  File "/base/data/home/apps/s~bnano-www/wtform.356204665143172839/webapp2_extras/jinja2.py", line 158, in render_template
    return self.environment.get_template(_filename).render(**context)
  File "/base/data/home/apps/s~bnano-www/wtform.356204665143172839/jinja2/environment.py", line 894, in render
    return self.environment.handle_exception(exc_info, True)
  File "template_files/details.html", line 78, in top-level template code
    <dd> {{ form.address|safe }} </dd>
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 10: ordinal not in range(128)

我的密码很普通:

^{pr2}$

我想知道我能做些什么来调整这个问题?非常感谢任何帮助。在

更新

实验上,我发现这样做可以处理错误: result.result_str = unicode(my_string_variable, "utf8") 我仍然希望避免操纵我使用的所有字符串。在

求解(?)在

上次我检查时没有出现问题。我怀疑这是因为我的测试数据是拉丁1的问题,所以我清除了我的测试数据。谢谢你的帮助。在


Tags: appsinpyselfhomedatabasereturn
2条回答

是的,这是一个编码错误。我看你已经试过添加.decode('utf-8')。但是您是否尝试过对导致错误的字符串进行编码,例如.encode('utf-8')?在

我也用GAE和Jinja2。我碰巧用西里尔字母符号发现了这个错误。 所以这3个字符串帮助了我(我不想在代码中手动“.encode”我所有的字符串)。在

import sys
reload(sys)
sys.setdefaultencoding('utf-8')

这是Richard Huang在这里指出的https://stackoverflow.com/a/14919377/605952

相关问题 更多 >