尝试将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编解码器并失败?
答案 0 :(得分:6)
我在我的项目中使用了以下助手
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')