【问题标题】:Trying to print nested dictionary value尝试打印嵌套字典值
【发布时间】:2019-03-19 15:55:47
【问题描述】:

我有这个来自 arista 开关的嵌套字典输出,并且想要迭代这个 json 列表/字典以提取每个接口的以下信息。

Interfacename(like Ethernet29/1)actorPortPriority(32768)

如何访问这些数据?

我尝试过:

for k, v in data.items():
    print(v['interfaces'])
    print(v['interfaces']['actorPortPriority']

但我没听懂。 据我了解,我有一个嵌套的字典,但实际上并没有得到它 遍历嵌套的dicts。

data 的示例:

{'interfaces': {'Ethernet29/1': {'actorOperKey': '0x00c8',
                                 'actorPortId': 117,
                                 'actorPortPriority': 32768,
                                 'actorPortState': {'activity': True,
                                                    'aggregation': True,
                                                    'collecting': True,
                                                    'defaulted': False,
                                                    'distributing': True,
                                                    'expired': False,
                                                    'synchronization': True,
                                                    'timeout': False},
                                 'actorPortStatus': 'bundled',
                                 'partnerOperKey': '0x82af',
                                 'partnerPortId': 16643,
                                 'partnerPortPriority': 32768,
                                 'partnerPortState': {'activity': True,
                                                      'aggregation': True,
                                                      'collecting': True,
                                                      'defaulted': False,
                                                      'distributing': True,
                                                      'expired': False,
                                                      'synchronization': True,
                                                      'timeout': False},
                                 'partnerSysId': '7F9B,00-23-04-ee-be-cc'},
                'Ethernet30/1': {'actorOperKey': '0x00c8',
                                 'actorPortId': 121,
                                 'actorPortPriority': 32768,
                                 'actorPortState': {'activity': True,
                                                    'aggregation': True,
                                                    'collecting': True,
                                                    'defaulted': False,
                                                    'distributing': True,
                                                    'expired': False,
                                                    'synchronization': True,
                                                    'timeout': False},
                                 'actorPortStatus': 'bundled',
                                 'partnerOperKey': '0x82af',
                                 'partnerPortId': 259,
                                 'partnerPortPriority': 32768,
                                 'partnerPortState': {'activity': True,
                                                      'aggregation': True,
                                                      'collecting': True,
                                                      'defaulted': False,
                                                      'distributing': True,
                                                      'expired': False,
                                                      'synchronization': True,
                                                      'timeout': False},
                                 'partnerSysId': '7F9B,00-23-04-ee-be-cc'}},
 'markers': {'markers': ['*']}}
{'interfaces': {'Ethernet16/1': {'actorOperKey': '0x0385',
                                 'actorPortId': 65,
                                 'actorPortPriority': 32768,
                                 'actorPortState': {'activity': True,
                                                    'aggregation': True,
                                                    'collecting': True,
                                                    'defaulted': False,
                                                    'distributing': True,
                                                    'expired': False,
                                                    'synchronization': True,
                                                    'timeout': False},
                                 'actorPortStatus': 'bundled',
                                 'partnerOperKey': '0x0001',
                                 'partnerPortId': 49,
                                 'partnerPortPriority': 32768,
                                 'partnerPortState': {'activity': True,
                                                      'aggregation': True,
                                                      'collecting': True,
                                                      'defaulted': False,
                                                      'distributing': True,
                                                      'expired': False,
                                                      'synchronization': True,
                                                      'timeout': False},
                                 'partnerSysId': '8000,28-99-3a-fa-50-fc'}},
 'markers': {'markers': ['*']}}
{'interfaces': {'Ethernet11/1': {'actorOperKey': '0x0067',
                                 'actorPortId': 45,
                                 'actorPortPriority': 0,
                                 'actorPortState': {'activity': False,
                                                    'aggregation': True,
                                                    'collecting': False,
                                                    'defaulted': True,
                                                    'distributing': False,
                                                    'expired': False,
                                                    'synchronization': False,
                                                    'timeout': False},
                                 'actorPortStatus': 'noAgg',
                                 'partnerOperKey': '0x0000',
                                 'partnerPortId': 0,
                                 'partnerPortPriority': 0,
                                 'partnerPortState': {'activity': False,
                                                      'aggregation': False,
                                                      'collecting': False,
                                                      'defaulted': False,
                                                      'distributing': False,
                                                      'expired': False,
                                                      'synchronization': False,
                                                      'timeout': True},
                                 'partnerSysId': '0000,00-00-00-00-00-00'},
                'Ethernet11/2': {'actorOperKey': '0x0067',
                                 'actorPortId': 46,
                                 'actorPortPriority': 32768,
                                 'actorPortState': {'activity': False,
                                                    'aggregation': True,
                                                    'collecting': False,
                                                    'defaulted': True,
                                                    'distributing': False,
                                                    'expired': False,
                                                    'synchronization': False,
                                                    'timeout': False},
                                 'actorPortStatus': 'noAgg',
                                 'partnerOperKey': '0x0000',
                                 'partnerPortId': 0,
                                 'partnerPortPriority': 0,
                                 'partnerPortState': {'activity': False,
                                                      'aggregation': False,
                                                      'collecting': False,
                                                      'defaulted': False,
                                                      'distributing': False,
                                                      'expired': False,
                                                      'synchronization': False,
                                                      'timeout': True},
                                 'partnerSysId': '0000,00-00-00-00-00-00'}},
 'markers': {'markers': ['*']}}

【问题讨论】:

  • 试试pprint 模块。如果您需要自己编写,则需要研究递归例程。
  • 上面代码的错误在这里print(v['interfaces']['actorPortPriority'])。您错过了密钥'Ethernet29/1'。 k 的值是“interfaces”,v 是interfaces 内的dict'Ethernet29/1' 是第一个键。所以,上面的代码应该是print(v['Ethernet29/1']['actorPortPriority'])

标签: python json loops dictionary


【解决方案1】:

您在最初的尝试中接近

假设 json 对象的数组名为data

for item in data:
    for k, v in item.get('interfaces').items():
        print('Interface Name: {} || Port Priority: {}'.format(k, v.get('actorPortPriority')))

Interface Name: Ethernet29/1 || Port Priority: 32768
Interface Name: Ethernet30/1 || Port Priority: 32768
Interface Name: Ethernet16/1 || Port Priority: 32768
Interface Name: Ethernet11/1 || Port Priority: 0
Interface Name: Ethernet11/2 || Port Priority: 32768

【讨论】:

    【解决方案2】:

    想象这就是你的输入数据的样子(因为你说的是​​ dict/list)

    data = [
        {
            "interfaces": {
                "Ethernet30/1": {
                    "partnerPortPriority": 32768, 
                    "partnerPortState": {
                        "collecting": true, 
                        "distributing": true, 
                        "synchronization": true, 
                        "defaulted": false, 
                        "timeout": false, 
                        "activity": true, 
                        "expired": false, 
                        "aggregation": true
                    }, 
                    "actorPortPriority": 32768, 
                    "actorPortState": {
                        "collecting": true, 
                        "distributing": true, 
                        "synchronization": true, 
                        "defaulted": false, 
                        "timeout": false, 
                        "activity": true, 
                        "expired": false, 
                        "aggregation": true
                    }, 
                    "partnerOperKey": "0x82af", 
                    "partnerSysId": "7F9B,00-23-04-ee-be-cc", 
                    "actorPortStatus": "bundled", 
                    "actorOperKey": "0x00c8", 
                    "partnerPortId": 259, 
                    "actorPortId": 121
                }, 
                "Ethernet29/1": {
                    "partnerPortPriority": 32768, 
                    "partnerPortState": {
                        "collecting": true, 
                        "distributing": true, 
                        "synchronization": true, 
                        "defaulted": false, 
                        "timeout": false, 
                        "activity": true, 
                        "expired": false, 
                        "aggregation": true
                    }, 
                    "actorPortPriority": 32768, 
                    "actorPortState": {
                        "collecting": true, 
                        "distributing": true, 
                        "synchronization": true, 
                        "defaulted": false, 
                        "timeout": false, 
                        "activity": true, 
                        "expired": false, 
                        "aggregation": true
                    }, 
                    "partnerOperKey": "0x82af", 
                    "partnerSysId": "7F9B,00-23-04-ee-be-cc", 
                    "actorPortStatus": "bundled", 
                    "actorOperKey": "0x00c8", 
                    "partnerPortId": 16643, 
                    "actorPortId": 117
                }
            }, 
            "markers": {
                "markers": [
                    "*"
                ]
            }
        }, 
        {
            "interfaces": {
                "Ethernet16/1": {
                    "partnerPortPriority": 32768, 
                    "partnerPortState": {
                        "collecting": true, 
                        "distributing": true, 
                        "synchronization": true, 
                        "defaulted": false, 
                        "timeout": false, 
                        "activity": true, 
                        "expired": false, 
                        "aggregation": true
                    }, 
                    "actorPortPriority": 32768, 
                    "actorPortState": {
                        "collecting": true, 
                        "distributing": true, 
                        "synchronization": true, 
                        "defaulted": false, 
                        "timeout": false, 
                        "activity": true, 
                        "expired": false, 
                        "aggregation": true
                    }, 
                    "partnerOperKey": "0x0001", 
                    "partnerSysId": "8000,28-99-3a-fa-50-fc", 
                    "actorPortStatus": "bundled", 
                    "actorOperKey": "0x0385", 
                    "partnerPortId": 49, 
                    "actorPortId": 65
                }
            }, 
            "markers": {
                "markers": [
                    "*"
                ]
            }
        }, 
        {
            "interfaces": {
                "Ethernet11/2": {
                    "partnerPortPriority": 0, 
                    "partnerPortState": {
                        "collecting": false, 
                        "distributing": false, 
                        "synchronization": false, 
                        "defaulted": false, 
                        "timeout": true, 
                        "activity": false, 
                        "expired": false, 
                        "aggregation": false
                    }, 
                    "actorPortPriority": 32768, 
                    "actorPortState": {
                        "collecting": false, 
                        "distributing": false, 
                        "synchronization": false, 
                        "defaulted": true, 
                        "timeout": false, 
                        "activity": false, 
                        "expired": false, 
                        "aggregation": true
                    }, 
                    "partnerOperKey": "0x0000", 
                    "partnerSysId": "0000,00-00-00-00-00-00", 
                    "actorPortStatus": "noAgg", 
                    "actorOperKey": "0x0067", 
                    "partnerPortId": 0, 
                    "actorPortId": 46
                }, 
                "Ethernet11/1": {
                    "partnerPortPriority": 0, 
                    "partnerPortState": {
                        "collecting": false, 
                        "distributing": false, 
                        "synchronization": false, 
                        "defaulted": false, 
                        "timeout": true, 
                        "activity": false, 
                        "expired": false, 
                        "aggregation": false
                    }, 
                    "actorPortPriority": 0, 
                    "actorPortState": {
                        "collecting": false, 
                        "distributing": false, 
                        "synchronization": false, 
                        "defaulted": true, 
                        "timeout": false, 
                        "activity": false, 
                        "expired": false, 
                        "aggregation": true
                    }, 
                    "partnerOperKey": "0x0000", 
                    "partnerSysId": "0000,00-00-00-00-00-00", 
                    "actorPortStatus": "noAgg", 
                    "actorOperKey": "0x0067", 
                    "partnerPortId": 0, 
                    "actorPortId": 45
                }
            }, 
            "markers": {
                "markers": [
                    "*"
                ]
            }
        }
    ]
    

    我的解决方案是(如果你想在不同的字典/列表中)

    In [1]: output = []
        ...: for interface in data:
        ...:     output.append({k: v['actorPortPriority'] for k, v in interface['interfaces'].items()})
        ...:         
    
    In [2]: output
    Out[2]: 
    [{'Ethernet29/1': 32768, 'Ethernet30/1': 32768},
     {'Ethernet16/1': 32768},
     {'Ethernet11/1': 0, 'Ethernet11/2': 32768}]
    

    如果你想在一个字典中

    In [3]: output = {}
        ...: for interface in data:
        ...:     output.update({k: v['actorPortPriority'] for k, v in interface['interfaces'].items()})
        ...:         
    
    In [4]: output
    Out[4]: 
    {'Ethernet11/1': 0,
     'Ethernet11/2': 32768,
     'Ethernet16/1': 32768,
     'Ethernet29/1': 32768,
     'Ethernet30/1': 32768}
    

    【讨论】:

      【解决方案3】:

      假设数据实际上是list,看起来像这样:

      data = [
          {'interfaces': {'Ethernet29/1': {'actorOperKey': '0x00c8',
                                           'actorPortId': 117,
                                           'actorPortPriority': 32768,
                                           'actorPortState': {'activity': True,
                                                              'aggregation': True,
                                                              'collecting': True,
                                                              'defaulted': False,
                                                              'distributing': True,
                                                              'expired': False,
                                                              'synchronization': True,
                                                              'timeout': False},
                                           'actorPortStatus': 'bundled',
                                           'partnerOperKey': '0x82af',
                                           'partnerPortId': 16643,
                                           'partnerPortPriority': 32768,
                                           'partnerPortState': {'activity': True,
                                                                'aggregation': True,
                                                                'collecting': True,
                                                                'defaulted': False,
                                                                'distributing': True,
                                                                'expired': False,
                                                                'synchronization': True,
                                                                'timeout': False},
                                           'partnerSysId': '7F9B,00-23-04-ee-be-cc'},
                          'Ethernet30/1': {'actorOperKey': '0x00c8',
                                           'actorPortId': 121,
                                           'actorPortPriority': 32768,
                                           'actorPortState': {'activity': True,
                                                              'aggregation': True,
                                                              'collecting': True,
                                                              'defaulted': False,
                                                              'distributing': True,
                                                              'expired': False,
                                                              'synchronization': True,
                                                              'timeout': False},
                                           'actorPortStatus': 'bundled',
                                           'partnerOperKey': '0x82af',
                                           'partnerPortId': 259,
                                           'partnerPortPriority': 32768,
                                           'partnerPortState': {'activity': True,
                                                                'aggregation': True,
                                                                'collecting': True,
                                                                'defaulted': False,
                                                                'distributing': True,
                                                                'expired': False,
                                                                'synchronization': True,
                                                                'timeout': False},
                                           'partnerSysId': '7F9B,00-23-04-ee-be-cc'}},
           'markers': {'markers': ['*']}},
           {'interfaces': {'Ethernet16/1': {'actorOperKey': '0x0385',
                                           'actorPortId': 65,
                                           'actorPortPriority': 32768,
                                           'actorPortState': {'activity': True,
                                                              'aggregation': True,
                                                              'collecting': True,
                                                              'defaulted': False,
                                                              'distributing': True,
                                                              'expired': False,
                                                              'synchronization': True,
                                                              'timeout': False},
                                           'actorPortStatus': 'bundled',
                                           'partnerOperKey': '0x0001',
                                           'partnerPortId': 49,
                                           'partnerPortPriority': 32768,
                                           'partnerPortState': {'activity': True,
                                                                'aggregation': True,
                                                                'collecting': True,
                                                                'defaulted': False,
                                                                'distributing': True,
                                                                'expired': False,
                                                                'synchronization': True,
                                                                'timeout': False},
                                           'partnerSysId': '8000,28-99-3a-fa-50-fc'}},
           'markers': {'markers': ['*']}},
           {'interfaces': {'Ethernet11/1': {'actorOperKey': '0x0067',
                                           'actorPortId': 45,
                                           'actorPortPriority': 0,
                                           'actorPortState': {'activity': False,
                                                              'aggregation': True,
                                                              'collecting': False,
                                                              'defaulted': True,
                                                              'distributing': False,
                                                              'expired': False,
                                                              'synchronization': False,
                                                              'timeout': False},
                                           'actorPortStatus': 'noAgg',
                                           'partnerOperKey': '0x0000',
                                           'partnerPortId': 0,
                                           'partnerPortPriority': 0,
                                           'partnerPortState': {'activity': False,
                                                                'aggregation': False,
                                                                'collecting': False,
                                                                'defaulted': False,
                                                                'distributing': False,
                                                                'expired': False,
                                                                'synchronization': False,
                                                                'timeout': True},
                                           'partnerSysId': '0000,00-00-00-00-00-00'},
                          'Ethernet11/2': {'actorOperKey': '0x0067',
                                           'actorPortId': 46,
                                           'actorPortPriority': 32768,
                                           'actorPortState': {'activity': False,
                                                              'aggregation': True,
                                                              'collecting': False,
                                                              'defaulted': True,
                                                              'distributing': False,
                                                              'expired': False,
                                                              'synchronization': False,
                                                              'timeout': False},
                                           'actorPortStatus': 'noAgg',
                                           'partnerOperKey': '0x0000',
                                           'partnerPortId': 0,
                                           'partnerPortPriority': 0,
                                           'partnerPortState': {'activity': False,
                                                                'aggregation': False,
                                                                'collecting': False,
                                                                'defaulted': False,
                                                                'distributing': False,
                                                                'expired': False,
                                                                'synchronization': False,
                                                                'timeout': True},
                                           'partnerSysId': '0000,00-00-00-00-00-00'}},
           'markers': {'markers': ['*']}}
      ]
      

      你可以使用:

      for v in data:
          for interface, info in v['interfaces'].items():
              print(interface)
              print(info['actorPortPriority'])
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-05-26
        • 2021-06-01
        • 2021-12-11
        • 2021-12-06
        • 2018-09-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多