Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

dict = urllib.urldecode(string) ?

0 views
Skip to first unread message

Joel Bender

unread,
Jun 12, 2002, 2:23:53 PM6/12/02
to
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'}
>>>

Oleg Broytmann

unread,
Jun 12, 2002, 2:57:29 PM6/12/02
to

You cannot handle URLs like "city=London&city=Paris". BTW, modern
urlencode accept a list of 2-lists, not only a dict.

Oleg.
--
Oleg Broytmann http://phd.pp.ru/ p...@phd.pp.ru
Programmers don't die, they just GOSUB without RETURN.


Joel Bender

unread,
Jun 12, 2002, 4:08:57 PM6/12/02
to
Well, after a little more digging I found cgi.parse_qsl() which does
what I wanted to do and more, and the resulting list is can be passed
into urlencode. Thanks for the help Oleg.
0 new messages