okay, so according to http://docs.python.org/library/urllib.html
"The order of parameters in the encoded string will match the order of parameter tuples in the sequence."
except when I try to run this code:
import urllib
values ={'one':'one',
'two':'two',
'three':'three',
'four':'four',
'five':'five',
'six':'six',
'seven':'seven'}
data=urllib.urlencode(values)
print data
outputs as ...
seven=seven&six=six&three=three&two=two&four=four&five=five&one=one
7,6,3,2,4,5,1?
That doesn't look like the order of my tuples.
Dictionaries are inherently unordered because of the way they are implemented. If you want them to be ordered, you should use a list of tuples instead (or a tuple of lists, or a tuple of tuples, or a list of lists...):
values = [ ('one', 'one'), ('two', 'two') ... ]
Just in case someones arrives here like me searching for a way to get deterministic results from urlencode
, to encode the values alphabetically you can do it like this:
from urllib.parse import urlencode
values ={'one':'one',
'two':'two',
'three':'three',
'four':'four',
'five':'five',
'six':'six',
'seven':'seven'}
sorted_values = sorted(values.items(), key=lambda val: val[0])
data=urlencode(sorted_values)
print(data)
#> 'five=five&four=four&one=one&seven=seven&six=six&three=three&two=two'
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With