0

我是 Chrome 扩展的新手,刚刚构建了一个弹出窗口,当通过 Javascript 提交时,它会将信息发送到 GAE 上的 Python 脚本,该脚本与数据一起使用。现在,只要我不使用 Ä、Ö、Ü 等特殊字符,一切都可以正常工作。当我使用这些字母时,我得到了错误:

Traceback (most recent call last):
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1535, in __call__
    rv = self.handle_exception(request, response, e)
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1529, in __call__
    rv = self.router.dispatch(request, response)
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1278, in     default_dispatcher
    return route.handler_adapter(request, response)
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 1102, in __call__
return handler.dispatch()
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 572, in dispatch
return self.handle_exception(e, self.app.debug)
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 570, in dispatch
return method(*args, **kwargs)
  File "/base/data/home/apps/s~google.com:finaggintel/1.368063289009985228/main.py", line 115, in post
t.title = self.request.get('title').encode('utf-8')
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 175, in get
param_value = self.get_all(argument_name)
  File "/python27_runtime/python27_lib/versions/third_party/webapp2-2.5.2/webapp2.py", line 212, in get_all
param_value = self.params.getall(argument_name)
  File "/python27_runtime/python27_lib/versions/third_party/webob-1.1.1/webob/multidict.py", line 327, in getall
return map(self._decode_value, self.multi.getall(self._encode_key(key)))
  File "/python27_runtime/python27_lib/versions/third_party/webob-1.1.1/webob/multidict.py", line 301, in _decode_value
value = value.decode(self.encoding, self.errors)
  File "/python27_runtime/python27_dist/lib/python2.7/encodings/utf_8.py", line 16, in decode
return codecs.utf_8_decode(input, errors, True)
UnicodeDecodeError: 'utf8' codec can't decode byte 0xdc in position 0: unexpected end of data    

坦率地说 - 我不知道在哪里调试这个问题。我在 Python 中尝试了 utf-8 de- 和编码(但同样,这对我来说是新的):

class News(webapp2.RequestHandler):
def post(self):     
    try: 
        user_job = joblist[user][0]
        user_pod = joblist[user][1]
    except KeyError:
        user_job = 'Guest'
        user_pod = 'Guest'

    link = self.request.get('link').encode('utf-8')

    if 'http' not in self.request.get('link'):
        link ='http://'+self.request.get('link')
    else:
        link = self.request.get('link')

    t = NewsBase(parent=news_key('finaggnews'))
    t.user = user
    t.date = datetime.now()
    t.text = self.request.get('text').encode('utf-8')
    t.title = self.request.get('title').encode('utf-8')
    t.link = link
    t.upvotes = []
    t.downvotes = []
    t.put()

难道我做错了什么?我什至接近这个问题?谢谢你的帮助!

编辑:包括回溯

4

1 回答 1

1

好的,

你有它回到前面,你应该将传入数据解码为 un​​icode 表示。

例如

>>> x = "Ä"
>>> x.decode('utf-8')
u'\xc4'
>>> 
>>> y=x.decode('utf-8')
>>> print y
Ä
>>> 

所以对于你的线路

t.title = self.request.get('title').encode('utf-8')

尝试

t.title = self.request.get('title').decode('utf-8')

然而,这假设数据需要从 utf-8 流中解码。

您应该 accept-charset="utf-8"在表单中(或发布时在客户端上)指定,以便定义正确的编码,而不是猜测和尝试解码。

例如,在 Windows 上,默认编码不是 utf-8,而是 latin_1,并且尝试从 latin_1 解码 utf-8 是行不通的。如果使用 decode('latin_1') 可以解码 decode('utf-8') 失败的字符 (0xdc)

于 2013-06-14T09:47:16.693 回答