python - gae python ascii编解码器无法解码字节

标签 python google-app-engine

当我运行我的应用程序时,它会查找链接,检查它们是否存在于数据库中,并将它们添加到数据库中,但出现错误。

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

最佳答案

正如堆栈跟踪的最后一行所示,它首先尝试将值转换为 unicode,然后将其编码为 utf-8。但是,(隐式)转换使用的是 ascii,这对于您的字符串来说是不够的。在将其传递给 filter 之前,您可以尝试使用正确的编码自行将其转换为 unicode。示例:

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

(请记住,您需要提供正确的编码;如果name 不是UTF-8 字符串,而是Cp1252、ISO-Latin 或其他,您需要在 decode 调用中指定)

关于python - gae python ascii编解码器无法解码字节,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11789770/

相关文章:

google-app-engine - 我的 Java App Engine 托管虚拟机构建在 2015 年 4 月 14 日更新后未部署

python - Python与Matlab的通信

python - 如何在 word 文档中创建书签,然后使用 python 创建指向书签的内部超链接

Python 大型多列表高效查询

google-app-engine - Google App Engine 上的亚马逊产品广告 API

python - 为什么这个指令不起作用?

python - 为什么主线程在所有线程加入之前打印结束行

python - 在 python 中使用 BeautifulSoup 进行标签和字符串混合查找和替换

python - 如何在 Flask/GAE 中渲染标签?

java - 是否可以使用 Java 在 Google App Engine 上拥有计算属性?