gae python ascii编解码器无法解码字节2

时间:2012-08-04 16:59:40

标签: python google-app-engine google-cloud-datastore

当我尝试在Google App Engine中的数据库中存储信息时,我收到此错误

Traceback (most recent call last):
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1536, in __call__
    rv = self.handle_exception(request, response, e)
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1530, in __call__
    rv = self.router.dispatch(request, response)
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "/base/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.1/webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "/base/data/home/apps/s~nothing-but-net/1.360803849981021046/main.py", line 460, in get
    blchrlinks(True, a)
  File "/base/data/home/apps/s~nothing-but-net/1.360803849981021046/main.py", line 282, in blchrlinks
    b.put()
  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 1074, in put
    return datastore.Put(self._entity, **kwargs)
  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/datastore.py", line 579, in Put
    return PutAsync(entities, **kwargs).get_result()
  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/datastore.py", line 556, in PutAsync
    return _GetConnection().async_put(config, entities, local_extra_hook)
  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/datastore/datastore_rpc.py", line 1552, in async_put
    pbs = [self.__adapter.entity_to_pb(entity) for entity in entities]
  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/datastore.py", line 344, in entity_to_pb
    return entity._ToPb()
  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/datastore.py", line 1025, in _ToPb
    properties = datastore_types.ToPropertyPb(name, values)
  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/datastore_types.py", line 1745, in ToPropertyPb
    pbvalue = pack_prop(name, v, pb.mutable_value())
  File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/datastore_types.py", line 1556, in PackString
    pbvalue.set_stringvalue(unicode(value).encode('utf-8'))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 57: ordinal not in range(128)

2 个答案:

答案 0 :(得分:1)

您正在尝试使用ASCII编解码器解码非ASCII字符。您需要知道输入字符的编码方式。

UnicodeDecodeError: 'ascii' codec can't decode byte 0xc2 in position 57: ordinal not in range(128)

与普遍看法相反,二元数据没有内在意义。它只具有作者指定的含义。

答案 1 :(得分:1)

听起来你有一个unicode编码错误,但你没有足够的信息来帮助找出问题所在。

要尝试两件事。

1。)# -*- encoding: utf-8 -*-位于正在进行调用的文件的顶部(如果文件中有unicode,例如☃或€

2。)确保你发送unicode到db而不是str。否则会发生这种情况

>>> x = eval("'Capit\\xc3\\xa1n\\n'")
>>> x 
'Capit\xc3\xa1n\n'
>>> x[5]
'\xc3'
>>> len(x[5])
1

正如你所看到的那样,将多位unicode转换为2个字符,其中第一个超出了ascii生成的范围,因为它是utf-8编码位之一(这就是为什么正常的ascii主要工作的原因)在utf-8)

但请提供更多信息,我们可以看看。