Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does urllib.urlencode add square brackets and single quotes to my dict values?

Tags:

python

I'm trying to write a script that can parse a url and get the query params from it. So far I've mostly got it, but urllib.urlencode is adding square brackets and single quotes to my values when I think it shouldn't be. This is most likely a misunderstanding on my part, so if someone could explain why this happens and how to avoid it I would really appreciate it. Here is my script:

#/usr/bin/python
import urlparse
import urllib

url = 'https://mysite.com?Action=ParseUrl'
parsed_url = urlparse.urlparse(url)
query_params = urlparse.parse_qs(parsed_url.query)

query_string = urllib.urlencode(query_params)
print query_string

The resulting output is:

Action=%5B%27ParseUrl%27%5D

So you can see that the value of 'Action' has been surrounded by urlencoded [' '] characters. I would like the value of my query_string variable to be :

Action=ParseUrl

What can I do to my script to make this happen. I realize I could hack some sort of reg exp to remove the characters, but I would rather understand the fundamental reason of why this is not working the way I want it to so I can avoid this problem in the future.

Thanks!

like image 625
Eric N Avatar asked Feb 24 '14 18:02

Eric N


People also ask

What does Urllib parse URL encode do?

parse. urlencode() method can be used for generating the query string of a URL or data for a POST request.

What does Urllib quote do?

What does Urllib quote do? The quote() function encodes space characters to %20 . If you want to encode space characters to plus sign ( + ), then you can use another function named quote_plus provided by urllib.

What is URL encode in Python?

urlencode() function. This is a convenience function which takes a dictionary of key value pairs or a sequence of two-element tuples and uses the quote_plus() function to encode every value. The resulting string is a series of key=value pairs separated by & character.


1 Answers

See the documentation notes about parse_qs (emphasis added):

Parse a query string given as a string argument (data of type application/x-www-form-urlencoded). Data are returned as a dictionary. The dictionary keys are the unique query variable names and the values are lists of values for each name.

You can confirm this yourself:

>>> print query_params
{'Action': ['ParseUrl']}

Consider using parse_qsl instead. With that change, the rest of your script behaves as you intended.

like image 139
Rob Kennedy Avatar answered Sep 19 '22 13:09

Rob Kennedy