2012-08-03 103 views
1

当我运行找到链接的应用程序检查它们是否存在于数据库中并将它们添加到数据库时,出现错误。gae python ascii codec can not decode byte

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.360769209191920043/main.py", line 460, in get 
    blchrlinks(True, a) 
    File "/base/data/home/apps/s~nothing-but-net/1.360769209191920043/main.py", line 271, in blchrlinks 
    if Articles.by_name(title): 
    File "/base/data/home/apps/s~nothing-but-net/1.360769209191920043/main.py", line 498, in by_name 
    u = Articles.all().filter("name =", name).get() 
    File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 2099, in get 
    results = self.run(limit=1, **kwargs) 
    File "/base/python27_runtime/python27_lib/versions/1/google/appengine/ext/db/__init__.py", line 2063, in run 
    iterator = raw_query.Run(**kwargs) 
    File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/datastore.py", line 1622, in Run 
    itr = Iterator(self.GetBatcher(config=config)) 
    File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/datastore.py", line 1601, in GetBatcher 
    return self.GetQuery().run(_GetConnection(), query_options) 
    File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/datastore.py", line 1490, in GetQuery 
    filter_predicate=self.GetFilterPredicate(), 
    File "/base/python27_runtime/python27_lib/versions/1/google/appengine/api/datastore.py", line 1534, in GetFilterPredicate 
    property_filters.append(datastore_query.make_filter(name, op, values)) 
    File "/base/python27_runtime/python27_lib/versions/1/google/appengine/datastore/datastore_query.py", line 107, in make_filter 
    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) 

我对by_name代码:

def by_name(cls, name): 
    u = Articles.all().filter("name =", name).get() 
    return u 

回答

4

由于堆栈跟踪显示的最后一行,它试图将值转换为Unicode,然后再对其进行编码为UTF-8。但是,(隐式)转换使用ascii,这对于您的字符串不够。你可以尝试使用正确的编码将它转换为unicode,然后传递给filter。例如:

u = Articles.all().filter("name =", name.decode('utf-8')).get() 

(记住,你需要提供正确编码;如果name不是UTF-8字符串,而是一个的Cp1252,ISO-拉丁文或别的东西,你需要指定的decode致电)