How docs for urlencode tell me
The urlparse module provides the parse_qs () and parse_qsl () functions that are used to parse query strings into Python data structures.
(In older versions of Python, they were in the cgi module). So for example:
>>> import urllib >>> import urlparse >>> d = {'a':'b', 'c':'d'} >>> s = urllib.urlencode(d) >>> s 'a=b&c=d' >>> d1 = urlparse.parse_qs(s) >>> d1 {'a': ['b'], 'c': ['d']}
The obvious difference between the original dictionary d and the "round-tripped" one d1 is that the latter has (in this case, one element) lists as values โโ- because there is no guarantee of uniqueness in the query strings, and it may be important that your application found out that for each key several values โโwere set (that is, lists will not always be single-user ;-).
As an alternative:
>>> sq = urlparse.parse_qsl(s) >>> sq [('a', 'b'), ('c', 'd')] >>> dict(sq) {'a': 'b', 'c': 'd'}
you can get a sequence of pairs (urlencode also accepts such an argument - in this case it keeps order, while in the case of dict there is no order to save ;-). If you know that there are no duplicate "keys", or they donโt care, then (as I showed) you can call dict to get a dictionary with values โโother than the list. In general, however, you need to consider what you want to do if duplicates are present (Python does not decide on this on your behalf;).
Alex Martelli Aug 22 '10 at 19:02 2010-08-22 19:02
source share