Keeping Url Parameters In Order When Encoding With Urllib
I am trying to simulate a get request with python. I have a dictionary of parameters and am using urllib.urlencode to urlencode them I notice that although the dictionary is of the
Solution 1:
As you can see in the comments, Python dictionaries are not ordered, but there is an OrderedDict
in Python that you can use to achieve the result you want:
from collections import OrderedDict
import urllib
urllib.urlencode(OrderedDict([('k1', 'v1'), ('k2', 'v2'), ('k3', 'v3')]))
# Out: 'k1=v1&k2=v2&k3=v3'
More info about OrderedDIct
Post a Comment for "Keeping Url Parameters In Order When Encoding With Urllib"