尝试将Utf-8数据存储在数据存储区中以获取UnicodeEncodeError [英] Trying to store Utf-8 data in datastore getting UnicodeEncodeError

查看:88
本文介绍了尝试将Utf-8数据存储在数据存储区中以获取UnicodeEncodeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

试图将utf-8存入数据存储并得到错误:

 Traceback(最近一次调用最后一次):
File" / sinfo /google_appengine/google/appengine/ext/webapp/__init__.py"第511行,位于__call__
handler.get(* groups)
文件" /sinfo/siteinfo/siteinfo.py",行1911,获得
seoEntity.put()
文件/ sinfo / google_appengine / google / appengine / ext / db / __ init __。py,第833行放入
返回数据存储区。 Put(self._entity,rpc = rpc)
在放置
文件" /sinfo/google_appengine/google/appengine/api/datastore.py",275行中:req.entity_list()。extend [e._ToPb()for e in entities])
文件/sinfo/google_appengine/google/appengine/api/datastore.py" 680行,在_ToPb
properties = datastore_types.ToPropertyPb(名称,值)
在ToPropertyPb中的文件/sinfo/google_appengine/google/appengine/api/datastore_types.py" 1499行,
pbvalue = pack_prop(name,v,pb.mut ($)
文件/sinfo/google_appengine/google/appengine/api/datastore_types.py",第1322行,在PackString中
pbvalue.set_stringvalue(unicode(value).encode('utf- 8'))
UnicodeDecodeError:'ascii'编解码器无法解码位置1中的字节0xc3:序号不在范围内(128)



我如何解决这个问题?数据已经是utf-8编码,当我将它输入到数据存储区时,它使用ascii编解码器并失败了?

解决方案

在我的项目中使用以下帮助程序

  def force_utf8(string):
if type(string)== str:
返回字符串
返回string.encode('utf-8')

在传递给GAE之前,使用它来转义所有的unicode数据。您还可以在以下代码片段中找到有用的代码:

  def force_unicode(string):
if type(string)== unicode:
返回字符串
返回string.decode('utf-8')


Trying to store utf-8 into datastore and getting error :

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)

How do i solve this? The data is already utf-8 encoded and when I enter it into the datastore it uses the ascii codec and fails?

解决方案

I use following helper in my projects

def force_utf8(string):
    if type(string) == str:
        return string
    return string.encode('utf-8')

Use it to escape all your unicode data before passing to GAE. Also you can find useful the following snippet:

def force_unicode(string):
    if type(string) == unicode:
        return string
    return string.decode('utf-8')

这篇关于尝试将Utf-8数据存储在数据存储区中以获取UnicodeEncodeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆