gpt4 book ai didi

python - 是否可以通过 url 传递非 ASCII 值?或者这里还有其他我没有看到的错误?

转载 作者:太空宇宙 更新时间:2023-11-03 19:14:03 24 4
gpt4 key购买 nike

从这段代码中:

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 值吗?或者这里还有其他我没有看到的错误?

最佳答案

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

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

(并在获取给定_sentence时删除encode调用)。

关于python - 是否可以通过 url 传递非 ASCII 值?或者这里还有其他我没有看到的错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11995036/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com