dict = urllib.urldecode(string) ?

Oleg Broytmann phd at phd.pp.ru
Wed Jun 12 20:57:29 CEST 2002


On Wed, Jun 12, 2002 at 02:23:53PM -0400, Joel Bender wrote:
> 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'}

   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/            phd at phd.pp.ru
           Programmers don't die, they just GOSUB without RETURN.





More information about the Python-list mailing list