1

从此代码块:

class MainHandler(webapp2.RequestHandler):
    def get(self):

        template_values = {"given_sentence":'put a sentence here'}
        template = jinja_environment.get_template('index.html')
        self.response.out.write(template.render(template_values))

    def post(self):
        nb = naivebayes(getfeatures)
        sampletrain(nb)
        given_sentence = self.request.get("given_sentence").encode('utf-8')
      #  given_sentence = self.request.get("given_sentence")
        spam_result = nb.classify(given_sentence)
        submit_button = self.request.get("submit_button")
        if submit_button:
            self.redirect('/test_result?spam_result=%s&given_sentence=%s' % (spam_result, given_sentence))

我收到此错误:

  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1536, in __call__
    rv = self.handle_exception(request, response, e)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1530, in __call__
    rv = self.router.dispatch(request, response)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "C:\Users\CG\Desktop\Google Drive\Sci&Tech\projects\naivebayes\main.py", line 262, in post
    self.redirect('/test_result?spam_result=%s&given_sentence=%s' % (spam_result, given_sentence))
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 608, in redirect
    response=self.response)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1767, in redirect
    uri = str(urlparse.urljoin(request.url, uri))
UnicodeEncodeError: 'ascii' codec can't encode character u'\xe9' in position 82: ordinal not in range(128)
INFO     2012-08-16 20:02:52,229 dev_appserver.py:2952] "POST / HTTP/1.1" 500 -
INFO     2012-08-16 20:05:06,858 py_zipimport.py:148] zipimporter('C:\\Python27\\lib\\site-packages\\pyyaml-3.10-py2.7-win32.egg', '')
ERROR    2012-08-16 20:05:08,230 webapp2.py:1553] 'ascii' codec can't decode byte 0xc3 in position 16: ordinal not in range(128)
Traceback (most recent call last):
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1536, in __call__
    rv = self.handle_exception(request, response, e)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1530, in __call__
    rv = self.router.dispatch(request, response)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1278, in default_dispatcher
    return route.handler_adapter(request, response)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 1102, in __call__
    return handler.dispatch()
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 572, in dispatch
    return self.handle_exception(e, self.app.debug)
  File "C:\Program Files (x86)\Google\google_appengine\lib\webapp2\webapp2.py", line 570, in dispatch
    return method(*args, **kwargs)
  File "C:\Users\CG\Desktop\Google Drive\Sci&Tech\projects\naivebayes\main.py", line 262, in post
    self.redirect('/test_result?spam_result=%s&given_sentence=%s' % (spam_result, given_sentence))
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 16: ordinal not in range(128)

当 的值given_sentence是这样的:productos farmacéuticos comprar ahora.

我的python代码是utf-8。我还将我的模板(在<head>标签中)放在这一行:<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>.

问题似乎在于通过 url 传递一个非 ASCII 语句。如果我尝试这个(“productos comprar ahora”),没关系:

http://localhost:8084/test_result?spam_result=good&given_sentence=productos%20comprar%20ahora

但是如果我尝试这个:“productos farmacéuticos comprar ahora”我得到了上述错误。是不是可以通过 url 传递一个非 ASCII 值?还是这里有其他我没有看到的错误?

4

2 回答 2

1

这是一个基本的 Python 编码问题。您正在尝试将非 ASCII 字符串放入 ASCII 字符串中。改用 unicode(注意u):

self.redirect(u'/test_result?spam_result=%s&given_sentence=%s' % (spam_result, given_sentence))

(并放弃encode获取 given_sentence 的电话)。

于 2012-08-16T20:44:52.907 回答
0

是不是可以通过 url 传递一个非 ASCII 值?

不,这不对。这里的问题不是 Python,而是 URL/URI 规范RFC 3986(参见第 2.0-2.3 节)。

URL 只能包含来自 ASCII 受限子集的字符。

但是有一种方法可以将任意字节流作为 URL 的一部分传递:百分比编码(第 2.4 节)。因此,如果您有 UTF-8 字节字符串“farmac\xc3\xa9uticos”,则可以将其发送为“farmac%C3%A9uticos”。

如果您使用 urllib.urlencode 从 dict 中生成查询字符串,而不是尝试手动执行,它会为您解决这个问题。

但是如果你有 Unicode 字符串“farmacéuticos”呢?好吧,您必须将其编码为 UTF-8 或其他一些字节字符串编码,以便您可以对其进行百分比编码并通过 URL 传递它。另一端的服务器必须知道它正在获取 UTF-8(在 URL 解码之后),以便它可以重建您的 Unicode 字符串。

于 2012-08-16T21:04:12.883 回答