我需要在列表中的下列词典列表中搜索键name
,并且需要为该特定词典获取uuid
的值
body = '''{
"operatingsystems":[
{
"uuid":"2baae445-eec5-41c4-a373-05f0125d4dc0",
"name":"my node",
"description":"my node is compute node",
"status":"",
"provision_capabilities":[
"controller"
],
"hostname":"9.37.75.49",
"os_name":"Linux",
"os_type":"RHEL",
"version":"6.5",
"architecture":"x86_64",
"is_hypervisor":"false",
"hypervisor_type":null,
"hypervisor_id":null,
"total_memory":16334340,
"processor_speed":null,
"processor_num_cores":null,
"max_storage":"",
"links":[
{
"href":"/v1/5e9dc1e2a6864739b278f0b67e6229c6/operatingsystems/2baae445-eec5-41c4-a373-05f0125d4dc0",
"rel":"self"
},
{
"href":"5e9dc1e2a6864739b278f0b67e6229c6/operatingsystems/2baae445-eec5-41c4-a373-05f0125d4dc0",
"rel":"bookmark"
}
],
"created":"2014-04-16T05:44:04.327626",
"updated":"2014-04-16T06:45:58.181892"
},
{
"uuid":"0fcc39c1-1956-41b9-97c8-8462edbb8a46",
"name":"OpsMgmt node3",
"description":"Operating system for pool3",
"status":"",
"provision_capabilities":[
"sdnve_controller"
],
"hostname":"9.37.75.121",
"os_name":"Linux",
"os_type":"RHEL",
"version":"6.5",
"architecture":"x86_64",
"is_hypervisor":false,
"hypervisor_type":null,
"hypervisor_id":null,
"total_memory":3923764,
"processor_speed":null,
"processor_num_cores":null,
"max_storage":"",
"links":[
{
"href":"/v1/5e9dc1e2a6864739b278f0b67e6229c6/operatingsystems/0fcc39c1-1956-41b9-97c8-8462edbb8a46",
"rel":"self"
},
{
"href":"5e9dc1e2a6864739b278f0b67e6229c6/operatingsystems/0fcc39c1-1956-41b9-97c8-8462edbb8a46",
"rel":"bookmark"
}
],
"created":"2014-04-16T05:44:04.452042",
"updated":null
},
{
"uuid":"1d15250f-ec0b-42e9-9e61-4da92024e024",
"name":"OpsMgmt node",
"description":"Operating system for pool",
"status":"",
"provision_capabilities":[
"compute"
],
"hostname":"9.37.88.253",
"os_name":"Linux",
"os_type":"RHEL",
"version":"6.5",
"architecture":"x86_64",
"is_hypervisor":false,
"hypervisor_type":null,
"hypervisor_id":null,
"total_memory":8030692,
"processor_speed":null,
"processor_num_cores":null,
"max_storage":"",
"links":[
{
"href":"/v1/5e9dc1e2a6864739b278f0b67e6229c6/operatingsystems/1d15250f-ec0b-42e9-9e61-4da92024e024",
"rel":"self"
},
{
"href":"5e9dc1e2a6864739b278f0b67e6229c6/operatingsystems/1d15250f-ec0b-42e9-9e61-4da92024e024",
"rel":"bookmark"
}
],
"created":"2014-04-16T06:22:36.261333",
"updated":null
},
{
"uuid":"315efdbd-9537-4b32-8675-63cff8b2f9b2",
"name":"OpsMgmt node itself",
"description":"Operating system for pool which is same as opsmanagement",
"status":"",
"provision_capabilities":[
"connectivity"
],
"hostname":"9.37.75.129",
"os_name":"Linux",
"os_type":"RHEL",
"version":"6.5",
"architecture":"x86_64",
"is_hypervisor":false,
"hypervisor_type":null,
"hypervisor_id":null,
"total_memory":65977136,
"processor_speed":null,
"processor_num_cores":null,
"max_storage":"",
"links":[
{
"href":"/v1/5e9dc1e2a6864739b278f0b67e6229c6/operatingsystems/315efdbd-9537-4b32-8675-63cff8b2f9b2",
"rel":"self"
},
{
"href":"5e9dc1e2a6864739b278f0b67e6229c6/operatingsystems/315efdbd-9537-4b32-8675-63cff8b2f9b2",
"rel":"bookmark"
}
],
"created":"2014-04-16T07:11:56.258874",
"updated":null
},
{
"uuid":"5c907a69-0ad0-4a81-b64b-6c2b281004d6",
"name":"compute node",
"description":"Operating system for Compute",
"status":"",
"provision_capabilities":[
"compute"
],
"hostname":"9.37.89.12",
"os_name":"Linux",
"os_type":"RHEL",
"version":"6.5",
"architecture":"x86_64",
"is_hypervisor":false,
"hypervisor_type":null,
"hypervisor_id":null,
"total_memory":32850352,
"processor_speed":null,
"processor_num_cores":null,
"max_storage":"",
"links":[
{
"href":"/v1/5e9dc1e2a6864739b278f0b67e6229c6/operatingsystems/5c907a69-0ad0-4a81-b64b-6c2b281004d6",
"rel":"self"
},
{
"href":"5e9dc1e2a6864739b278f0b67e6229c6/operatingsystems/5c907a69-0ad0-4a81-b64b-6c2b281004d6",
"rel":"bookmark"
}
],
"created":"2014-04-17T09:08:17.967685",
"updated":null
}
]
}'''
答案 0 :(得分:2)
你有JSON数据;使用json
module来解析它。从那里开始,您可以访问operatingssystems
列表并搜索给定名称:
import json
data = json.loads(body)
name_to_find = 'foobar'
uuid = next((d['uuid']
for d in data['operatingsystems']
if d['name'] == name_to_find), None)
演示:
>>> import json
>>> data = json.loads(body)
>>> name_to_find = 'OpsMgmt node'
>>> next((d['uuid']
... for d in data['operatingsystems']
... if d['name'] == name_to_find), None)
u'1d15250f-ec0b-42e9-9e61-4da92024e024'
答案 1 :(得分:0)