【问题标题】:Parse list of dictionaries in a dictionary解析字典中的字典列表
【发布时间】:2014-04-17 13:32:01
【问题描述】:

我需要在下面的字典列表中搜索键 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
      }
   ]
}'''

【问题讨论】:

  • 向我们展示您的尝试。我们帮助解决错误.. 不做你的工作
  • 这个问题似乎是题外话,因为它是一个家庭作业转储类型的问题。
  • @ErikAllik 这太苛刻了,还不错。
  • @ErikAllik:它可能缺少一点努力,但它也不是一个家庭垃圾场。
  • @ErikAllik:很可能是因为他们一开始就不知道它是 JSON?一旦你知道了所有这些事情就会很容易,当你还在开始的时候你会忘记它是什么样的。

标签: python json dictionary


【解决方案1】:

你有 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'

【讨论】:

    【解决方案2】:

    这个工具可以帮助你。这是一个我觉得非常有用的在线 JSON 查看器。仅将此文本的 JSON 部分复制到查看器。

    Online JSON Viewer

    但下一次,请考虑更好地组织您的问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-03-30
      相关资源
      最近更新 更多