gpt4 book ai didi

python - 如何处理我的 UnicodeDecodeError?

转载 作者:行者123 更新时间:2023-11-28 20:51:42 29 4
gpt4 key购买 nike

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

'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)

我的代码很普通:

class UpdateForm(Form):       
address = TextField(_('Address'), [validators.Required()])
zipcode = TextField(_('Zip code'), [validators.Required()])
email = TextField(_('Email'), [validators.Required()])

class UpdateHandler(NewBaseHandler):

@user_required
def get(self):

user = auth_models.User.get_by_id(long(self.auth.get_user_by_session()['user_id']))

userinfo = self.auth.get_user_by_session()
if user.address:
address = user.address#.decode('utf-8')
logging.info('user info:'+str(userinfo))

if user:
data = {'user': user, 'address': address, 'form':UpdateForm(obj=user)}
self.render_jinja('details.html', **data)

@user_required
def post(self, sponsor=None):
user = \
auth_models.User.get_by_id(long(self.auth.get_user_by_session()['user_id'
]))
user.address = UpdateForm(self.request.params).address.data
user.put()
self.render_jinja('details.html', user=user, sponsor=sponsor, form=UpdateForm(obj=user))

我想知道我可以做些什么来调整这个问题?任何帮助是极大的赞赏。

更新

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

解决了(?)

我上次检查没有出现问题。我怀疑这是我的测试数据是 latin-1 的问题,所以我清除了我的测试数据。感谢您在这里提供的所有帮助。

最佳答案

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

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

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

关于python - 如何处理我的 UnicodeDecodeError?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8924014/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com