正在尝试打印嵌套字典值

2024-05-12 20:00:42 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个arista开关的嵌套字典输出,并希望迭代这个json列表/dict,为每个接口提取以下信息。你知道吗

Interfacename(like Ethernet29/1)actorPortPriority(32768)

如何访问这些数据?你知道吗

我试着做:

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

但我没有明白。 据我所知,我有一个嵌套的dict,但实际上我没有得到它 遍历嵌套的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': ['*']}}

Tags: falsetruetimeoutactivityinterfacessynchronizationaggregationdistributing
3条回答

想象一下这就是你输入数据的样子(因为你说的是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}

假设数据实际上是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'])

你与你最初的尝试非常接近。你知道吗

假设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

相关问题 更多 >