令人困惑的 python urlencode 顺序

发布于 2024-09-11 11:45:27 字数 614 浏览 18 评论 0原文

好的,根据 http://docs.python.org/library/urllib.html

“编码字符串中参数的顺序将与序列中参数元组的顺序匹配。”

除非我尝试运行此代码:

import urllib
values ={'one':'one',
         'two':'two',
         'three':'three',
         'four':'four',
         'five':'five',
         'six':'six',
         'seven':'seven'}
data=urllib.urlencode(values)
print data

输出为...

seven=seven&six=six&three=three&two=two&four=four&five=five&one=one

7,6,3,2,4,5,1?

这看起来不像我的元组的顺序。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

满天都是小星星 2024-09-18 11:45:27

由于字典的实现方式,它们本质上是无序的。如果您希望对它们进行排序,则应该使用元组列表(或列表元组,或元组元组,或列表列表...):

values = [ ('one', 'one'), ('two', 'two') ... ]

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') ... ]
傾城如夢未必闌珊 2024-09-18 11:45:27

以防万一有人像我一样到达这里寻找一种从 urlencode 获取确定性结果的方法,按字母顺序对值进行编码,您可以这样做:

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'

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'
不羁少年 2024-09-18 11:45:27

为什么不使用OrderedDict?您的代码将如下所示:

from collections import OrderedDict
from urllib.parse import urlencode

d = OrderedDict()
d['one'] = 'one'
d['two'] = 'two'
d['three'] = 'three'
d['four'] = 'four'
...

data=urlencode(d)
print(data)
# one=one&two=two&three=three&four=four

这样您的字典的顺序将被保留

Why not using OrderedDict? Your code would then look like this:

from collections import OrderedDict
from urllib.parse import urlencode

d = OrderedDict()
d['one'] = 'one'
d['two'] = 'two'
d['three'] = 'three'
d['four'] = 'four'
...

data=urlencode(d)
print(data)
# one=one&two=two&three=three&four=four

This way the order of your dictionary would be preserved

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文