尝试将 utf-8 存储到数据存储中并收到错误:
Traceback (most recent call last):
File "/sinfo/google_appengine/google/appengine/ext/webapp/__init__.py", line 511, in __call__
handler.get(*groups)
File "/sinfo/siteinfo/siteinfo.py", line 1911, in get
seoEntity.put()
File "/sinfo/google_appengine/google/appengine/ext/db/__init__.py", line 833, in put
return datastore.Put(self._entity, rpc=rpc)
File "/sinfo/google_appengine/google/appengine/api/datastore.py", line 275, in Put
req.entity_list().extend([e._ToPb() for e in entities])
File "/sinfo/google_appengine/google/appengine/api/datastore.py", line 680, in _ToPb
properties = datastore_types.ToPropertyPb(name, values)
File "/sinfo/google_appengine/google/appengine/api/datastore_types.py", line 1499, in ToPropertyPb
pbvalue = pack_prop(name, v, pb.mutable_value())
File "/sinfo/google_appengine/google/appengine/api/datastore_types.py", line 1322, in PackString
pbvalue.set_stringvalue(unicode(value).encode('utf-8'))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 1: ordinal not in range(128)
我该如何解决这个问题?数据已经是 utf-8 编码的,当我将其输入数据存储时,它使用 ascii 编解码器并失败?
我在我的项目中使用以下助手
def force_utf8(string):
if type(string) == str:
return string
return string.encode('utf-8')
在传递给 GAE 之前,使用它来转义所有 unicode 数据。您还可以找到有用的以下代码片段:
def force_unicode(string):
if type(string) == unicode:
return string
return string.decode('utf-8')
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)