如何处理我的 UnicodeDecodeError?

发布于 2024-12-27 17:23:39 字数 3664 浏览 0 评论 0原文

我的数据存储区包含值,我希望我的表单能够更新这些值。我在 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 的问题,所以我清除了测试数据。感谢您在这里的所有帮助。

My datastore contains values and I want my form to enable updating of those values. I use wtforms in jinja2 with google app engine. I get an error message that I cannot understand:

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

The code I have is ordinary:

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

I wonder what I can do to adjust the problem? Any help is greatly appreciated.

Update

Experimentally I found that doing this handled the error:
result.result_str = unicode(my_string_variable, "utf8")
I would still like to avoid having to manipulate all the strings I use though.

Solved(?)

Last time I checked the problem did not occur. I suspect this being a problem with my test data being latin-1 so I cleared my test data. Thanks for all the help here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笑忘罢 2025-01-03 17:23:39

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

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

Richard Huang 在这里指出了这一点 https://stackoverflow.com/a/14919377/605952

I use GAE and Jinja2 as well. And I happened to catch this error with cyrillic symbols.
So these 3 strings of code helped me out (and I didn't want to ".encode" all of my strings manually in the code).

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

This was pointed out by Richard Huang here https://stackoverflow.com/a/14919377/605952

玩世 2025-01-03 17:23:39

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

Yeah, it's an encoding error. I see you have already tried adding .decode('utf-8'). But have you tried to encode the string that cause the error like this .encode('utf-8')?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文