在Python / AppEngine中传递GET数据时的UnicodeDecodeError [英] UnicodeDecodeError when passing GET data in Python/AppEngine
问题描述
这感觉像是一个非常基本的问题,但我一直无法找到答案。
This feels like a really basic question, but I haven't been able to find an answer.
我想从一个url读取数据,例如从查询字符串获取数据。我在Python中使用 webapp
框架。我尝试了下面的代码,但由于我在Python / appengine上有一个初学者,所以我肯定做了一些错误的事情。
I would like to read data from an url, for example GET data from a querystring. I am using the webapp
framework in Python. I tried the following code, but since I've a total beginner at Python/appengine, I've certainly done something wrong.
class MainPage(webapp.RequestHandler):
def get(self):
self.response.out.write(self.request.get('data'))
application = webapp.WSGIApplication([('/', MainPage),('/search', Search),('/next', Next)],debug=False)
def main():
run_wsgi_app(application)
if __name__ == "__main__":
main()
在我的测试环境中进行测试时,URL http:// localhost /?data = test
只会在下面返回此错误消息。
When testing in my test environment, the URL http://localhost/?data=test
just returns this error message below. Without the querystring, it just displays a blank page as expected.
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd6 in position 40: ordinal not in range(128)
我做错了什么,我应该怎么做?
What am I doing wrong and what should I do instead?
推荐答案
您试着例如打印实际包含不同字符集数据的ASCII编码字符串。这可能发生,例如用Latin-1编码的数据。尝试使用
You try to e.g. print an ASCII coded string actually containing data of a different charset. This can happen e.g. with Latin-1 encoded data. Try converting your input to unicode using
unicoded = unicode(non_unicode_string, source_encoding)
其中 source_encoding
类似于'cp1252'
,'iso-8859-1'
等等,然后发送给输出。
where source_encoding
is something like 'cp1252'
, 'iso-8859-1'
etc., and sending this to output.
在这份HOWTO 上。有关Python支持的编码列表,请参阅此
Have a look at this HOWTO. For a list of encodings supported by Python, see this
这篇关于在Python / AppEngine中传递GET数据时的UnicodeDecodeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!