我有一个来自arista交换机的嵌套字典输出,并且想要迭代此json列表/字典以为每个接口提取以下信息。
Interfacename(like Ethernet29/1)
和actorPortPriority(32768)
如何访问此数据?
我试图做:
for k, v in data.items():
print(v['interfaces'])
print(v['interfaces']['actorPortPriority']
但是我不明白。
据我了解,我有一个嵌套的字典,但实际上并没有
遍历嵌套的dict
。
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': ['*']}}
答案 0 :(得分:0)
您的原始尝试已关闭。
假设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
答案 1 :(得分:0)
想象一下这就是您的输入数据的样子(因为您说的是字典/列表)
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}
答案 2 :(得分:0)
假设数据实际上是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'])