2010-06-22 34 views
3

試圖爲UTF-8存儲到數據存儲,並收到錯誤:嘗試存儲UTF-8數據的數據存儲越來越UnicodeEncodeError

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編解碼器並失敗了?

+1

你能弄清楚這一點嗎?我有同樣的問題! – mahmoud 2010-09-18 21:49:27

回答

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

我期待着做同樣的事情。我有一個應用程序引擎分配名稱='全部'。該字符串是utf-8編碼的。嘗試force_utf8()但仍然得到相同的錯誤「'ascii'編解碼器無法解碼字節」 – sohil 2012-12-18 11:12:34