Python 是否可以通过url传递非ASCII值?还是我在这里犯了其他错误';我没看见?
从这段代码中:Python 是否可以通过url传递非ASCII值?还是我在这里犯了其他错误';我没看见?,python,utf-8,jinja2,Python,Utf 8,Jinja2,从这段代码中: 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(
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)
当给定句子的值如下:productos farmacéuticos compar ahora
我的python代码是utf-8。我还将模板(在
标记中)放在这一行:
问题似乎在于通过url传递非ASCII语句。如果我试试这个(“productos Compar ahora”),没关系:
http://localhost:8084/test_result?spam_result=good&given_sentence=productos%20comprar%20ahora
但如果我试着说:“productos farmacéuticos Compar ahora”,我就得到了上面的错误。难道不可能通过url传递非ASCII值吗?或者这里还有其他我没有看到的错误吗?这是一个基本的Python编码问题。您正在尝试将非ASCII字符串转换为ASCII字符串。改用unicode(注意
u
):
(并在获得给定的句子时放弃
encode
调用)。这是一个基本的Python编码问题。您正在尝试将非ASCII字符串转换为ASCII字符串。改用unicode(注意u
):
(在得到给定的句子时,放下encode
调用)
难道不可能通过url传递非ASCII值吗
不,不是。这里的问题不是Python,而是URL/URI规范(参见第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字符串
难道不可能通过url传递非ASCII值吗
不,不是。这里的问题不是Python,而是URL/URI规范(参见第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字符串。可能是URL编码那个笨蛋!你应该仔细阅读
urllib
,urllib2
,和urlencode
。可能是urlencode吧!您应该阅读urllib
、urllib2
和urlencode
。除非他在文件中有明确的编码指令,或者以其他方式设置默认的python编码,否则他可能首先需要对源字符串执行解码('utf-8')
。他的示例和堆栈跟踪以及进一步的解释不匹配(无论对我来说编码如何,示例代码似乎都不起作用)。我不明白为什么。AFAIKWebApp2(实际上是处理请求内容的WebOb)在到达处理程序之前将所有请求参数解码为unicode。只要他的URL字符串也是unicode,就根本没有理由编码为字节。啊,我明白了。我认为他的字符串实际上是从他的代码外部获取的(他给出的例子对我来说是一个非常奇怪和特殊的字符串,需要硬编码),但如果它们是直接硬编码的,我看你是对的。再次感谢@DanielRoseman。你帮了我很多-又一次@DanielRoseman,但是如果我放弃encode
调用,我会得到以下错误:uri=str(urlparse.urljoin(request.url,uri))UnicodeEncodeError:'ascii'编解码器无法对103位的字符u'\xed'进行编码:序号不在范围(128)
如果我没有,我会得到另一个错误:第263行,在post self.redirect中(u'/test_result?spam_result=%s&给定_语句=%s%%(spam_result,给定_语句))UnicodeDecodeError:“ascii”编解码器无法解码第37位的字节0xc3:序号不在范围内(128)
。除非他在文件中有明确的编码指令,或者以其他方式设置默认的python编码,否则他可能需要首先对源字符串执行解码('utf-8')
。他的示例和堆栈跟踪以及进一步的解释不匹配(无论对我来说编码如何,示例代码似乎都不起作用)。我不明白为什么。AFAIK webapp2(实际上是WebOb,它处理请求内容)在到达处理程序之前,将所有请求参数解码为unicode。只要他的URL字符串也是unicode,就根本没有理由将其编码为字节。啊,我现在明白了。我认为他的字符串实际上是从他的代码外部获取的(他给出的示例是一个非常奇怪和特定的字符串,对我来说很难编码),但是如果它们是直接硬编码的,我看你是对的。@DanielRoseman,再次感谢你。你帮了我很多忙!@DanielRoseman,但是如果我放弃encode
调用,我会得到这个错误:uri=str(urlparse.urljoin(request.url,uri))UnicodeEncodeError:“ascii”编解码器无法对103位置的字符u'\xed'进行编码:序号不在范围(128)
如果不在范围内,则会出现另一个错误:第263行,在post self.redirect(u'/test_result?spam_result)中=
self.redirect(u'/test_result?spam_result=%s&given_sentence=%s' % (spam_result, given_sentence))