Python, opposite urllib.urlencode function

How to convert data after processing urllib.urlencode to dict? urllib.urldecode does not exist.

+85
python urllib
Aug 22 '10 at 18:59
source share
3 answers

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;).

+117
Aug 22 '10 at 19:02
source share

Python 3 code to solve Alex:

 >>> import urllib.parse >>> d = {'a':'b', 'c':'d'} >>> s = urllib.parse.urlencode(d) >>> s 'a=b&c=d' >>> d1 = urllib.parse.parse_qs(s) >>> d1 {'a': ['b'], 'c': ['d']} 

Alternative:

 >>> sq = urllib.parse.parse_qsl(s) >>> sq [('a', 'b'), ('c', 'd')] >>> dict(sq) {'a': 'b', 'c': 'd'} 

parse_qsl is reversible:

 >>> urllib.parse.urlencode(sq) 'a=b&c=d' 
+16
Apr 17 2018-12-12T00:
source share

urllib.unquote_plus() does what you want. It replaces% xx with their symbolic equivalent and replaces the plus signs with spaces.

Example:

 unquote_plus('/%7Ecandidates/?name=john+connolly') 

profitability

 '/~candidates/?name=john connolly'. 
+16
Feb 26 '14 at 15:36
source share



All Articles