简体   繁体   English

如何在python中获取已解析查询字符串的值?

[英]How do I get the values of a parsed query string in python?

querystring = cgi.parse_qs(decrypted_qs)

self.response.out.write(querystring) #output: {'param2': ['v2'], 'param1': ['v1']}

self.response.out.write(querystring.param2) #AttributeError: 'dict' object has no attribute 'param2'

The original dict: {'param1': 'v1', 'param2': 'v2'} 原始字典: {'param1': 'v1', 'param2': 'v2'}

So why isn't the second one working, what's the right way to access the values? 那么第二个为什么不起作用,访问值的正确方法是什么?

I'm running Python 2.5.2 on App Engine. 我在App Engine上运行Python 2.5.2。

Python dicts don't work like JavaScript dicts. Python字典不像JavaScript字典那样工作。 You need to use it the same way you access elements in a list: 您需要使用与访问列表中元素相同的方式使用它:

querystring['param2']

Don't attempt to extract query string parameters yourself in the first place - use your framework. 首先,不要尝试自己提取查询字符串参数-使用您的框架。 If you're using webapp, you can access query string parameters through the dict self.request.GET . 如果您使用的是webapp,则可以通过dict self.request.GET访问查询字符串参数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM