Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中声明body字典时,如何按原始顺序传递requests body_Python_Json_Dictionary_Python Requests - Fatal编程技术网

在python中声明body字典时,如何按原始顺序传递requests body

在python中声明body字典时,如何按原始顺序传递requests body,python,json,dictionary,python-requests,Python,Json,Dictionary,Python Requests,当我试图用一本大词典发送post请求时,我被卡住了。 这是代码 body = {"session":{"user":{"user_id":"user1"}, \ "equipment":{"equipment_id_alternate":{"alternate_equipment_id_type":"sms_address", \ "alternate_equipment_id":"equipmentid1"}, \ "extensions":[{"extension_type":"string

当我试图用一本大词典发送post请求时,我被卡住了。 这是代码

body = {"session":{"user":{"user_id":"user1"}, \
"equipment":{"equipment_id_alternate":{"alternate_equipment_id_type":"sms_address", \
"alternate_equipment_id":"equipmentid1"}, \
"extensions":[{"extension_type":"string","name":"device_type","value":"PC_Apple"}, \
{"extension_type":"string","name":"device_id", \
"value":"equipmentid1"}]}, \
"content":{"content_type":"http_broadcast_channel", \
"uri":"http://xyz/abc.m3u8", \
"extensions":[{"extension_type":"integer","name":"bandwidth","value":0}, \
{"extension_type":"string","name":"stream_quality","value":"HD"}, \
{"extension_type":"string","name":"session_type","value":"Live_Linear_Companion"}]}}, \
"sa_version":"sa_http_v_1_0_1","request_type":"setup_request", \
"authentication":{"auth_algorithm":"sa_hmac_token_v_1_0_1","auth_message_algorithm":"sa_http_auth_message_v_1_0_1", \
"auth_token_start":"2016-11-23T19:49:56Z","auth_token_expiry":"2016-11-30T19:49:56Z", \
"auth_token":"abdcwgegegegege"}}

header19 = {some keys and values}
api19 = requests.post(url19, json=body, headers=header19)
我想以声明的顺序传递请求体,如何实现它

我看到很多类似的问题和回答,说你可以使用collections.orderDict();但是,如果不使用collections.orderDict()声明每个键和值对,我不知道如何正确使用它

谢谢。

您可以试试:

from collections import OrderedDict

ordered_body = OrderedDict(body)

ordered_body
中的键顺序应与指定的相同。我认为这可以非常清楚地解释排序dict。

常规dict不会跟踪插入顺序,对其进行迭代会生成任意顺序的值。相比之下,在OrderedDict中,在创建迭代器时会记住并使用项的插入顺序

import collections


print 'Regular dictionary:'
d = {}
d['a'] = 'A'
d['b'] = 'B'
d['c'] = 'C'
d['d'] = 'D'
d['e'] = 'E'

for k, v in d.items():
   print k, v

print '\nOrderedDict:'
d = collections.OrderedDict()
d['a'] = 'A'
d['b'] = 'B'
d['c'] = 'C'
d['d'] = 'D'
d['e'] = 'E'


for k, v in d.items():

 print k, v
输出将是:

Regular dictionary:
a A
c C
b B
e E
d D
订购信息技术:

a A
b B
c C
d D
e E

我建议从问题正文中删除您的
auth_令牌
值:)@AminEtesamian,谢谢,实际上这是机器生成的令牌,不能在其他任何地方使用。无论如何都已更改。实际上,我不想为每个键和子键指定值。那将是痛苦的。事实上我在解释它背后的概念。mikeqfu的答案是better@AminEtesamian,你试过了吗?如果我尝试打印有序的正文,我仍然以随机顺序获得结果。请在此处检查接受的答案: