parse query string with urllib in Python 2.4

谁说我不能喝 提交于 2019-12-03 03:32:01

You have two options:

>>> cgi.parse_qs(qs)
{'second': ['4'], 'third': ['3'], 'first': ['1']}

or

>>> cgi.parse_qsl(qs)
[('first', '1'), ('second', '4'), ('third', '3')]

The values in the dict returned by cgi.parse_qs() are lists rather than strings, in order to handle the case when the same parameter is specified several times:

>>> qs = 'tags=python&tags=programming'
>>> cgi.parse_qs(qs)
{'tags': ['python', 'programming']}

this solves the annoyance:

d = dict(urlparse.parse_qsl( qs ) )

personally i would expect there two be a built in wrapper in urlparse. in most cases i wouldn't mind to discards the redundant parameter if such exist

import urlparse
qs = 'first=1&second=4&third=3&first=0'

print dict(urlparse.parse_qsl(qs))

OR

print urlparse.parse_qs(qs)
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!