Search code examples
python-2.7urllib2urlliburlencodeurlparse

Convert Python(2.7) dictionary to valid query params


So I've been looking around quite a bit so far, and found hits but nothing that has quite fit this bill IMO. What I want to be able to do is take a Python dict called 'result' (note that 'keyword_scores' is a list of dicts, inside the 'data' dict.):

>>> print result
>>> {   
        u'data': {   
            u'clean_table': u'true',
            u'keyword_scores': [   
                {   
                    u'keyword': u'edited for easy reading on SO',
                    u'score': 0.4054651081081644
                }
            ],
            u'user_id': u'2'
        }
    }

and, at least I think, use some combination of

urllib.urlencode()

and

urlparse.parse_qs()

to be able to concatenate as 'query_string' onto a URL for use inside:

urllib2.Request(host_domain + some_post_data_endpoint + query_string)

I only suggest these libraries since thats what I think I need to use based on my searching so far, but I am wide open with approaches and tools to use here. The problem I'm having is, for whatever reason, it seems like I need to urlencode() it before I call parse_qs(), but other suggestions say I need to call parse_qs() first before encoding. Doesn't seem like there's a clean-cut way to convert a dictionary thats not completely "flat" into query parameters for urllib2.Request() to use.

Thank you in advance!


Solution

  • I'm assuming you have some endpoint to which you want to post the data. Then you can just do the following and decode the url at the endpoint:

    import urllib
    result = {u'data': {u'clean_table': u'true',u'keyword_scores': [{u'keyword': u'edited for easy reading on SO', u'score': 0.4054651081081644}],u'user_id': u'2'}}
    result_string = urllib.urlencode(result)
    urllib2.Request(host_domain + some_post_data_endpoint + "?result=" + result_string)
    

    For example, if your endpoint is JS, you can just do the following to get result as the same dictionary:

    var url = new URL(window.location.href);
    var result = JSON.parse(url.searchParams.get("result"));