dict = urllib.urldecode(string) ?

Joel Bender jjb5 at cornell.edu
Wed Jun 12 20:23:53 CEST 2002


I'm processing GET and POST data and I would like the functional 
inverse of urllib.urlencode().  That is, split the string apart by
the '&' chars, split each of those by '=' and build a dict of the 
results.  The key and the value should be passed to unquote_plus().

Here is my solution, comments?

>>> def urldecode(s):
...     rslt = {}
...     for item in s.split('&'):
...         keyValue = item.split('=')
...         rslt[ urllib.unquote_plus(keyValue[0]) ] = 
urllib.unquote_plus(keyValue[1])
...     return rslt
... 

>>> urldecode('a=b+c&d%26e=%20f')
{'a': 'b c', 'd&e': ' f'}
>>>



More information about the Python-list mailing list