[英]How to pull certain values from dic with Python
我试图从这个 output 中提取一些特定的值:
{'GigabitEthernet': [{'name': '1', 'ip': {'address': {'primary': {'address': '192.168.200.200', 'mask': '255.255.255.0'}}, 'Cisco-IOS-XE-ospf:router-ospf': {'ospf': {'authentication': {'key-chain': 'sv-10599'}, 'message-digest-key': [{'id': 1, 'md5': {'auth-key': 'cisco'}}], 'network': {'point-to-point': [None]}}}}, 'mop': {'enabled': False, 'sysid': False}, 'Cisco-IOS-XE-ethernet:negotiation': {'auto': True}}, {'name': '2', 'shutdown': [None], 'mop': {'enabled': False, 'sysid': False}, 'Cisco-IOS-XE-ethernet:negotiation': {'auto': True}}, {'name': '3', 'shutdown': [None], 'mop': {'enabled': False, 'sysid': False}, 'Cisco-IOS-XE-ethernet:negotiation': {'auto': True}}]}
理想情况下,我想获得以下值:
千兆以太网
1
192.168.200.200
255.255.255.0
千兆以太网
2
关闭
for device_interface_type in device_config['Cisco-IOS-XE-native:native']['interface']:
print(device_interface_type)
返回 GigabitEthernet,但我似乎无法获得“名称”、“地址”或“掩码”。 对此有更多了解的人可以帮助我并指出我可以了解更多关于如何使用 python 处理这些情况的好位置吗?
谢谢!
使用get
function。 例如,使用以下字典:
test_dict = {'a': 1, 'b': 'two', 'c': 3.0}
test_dict.get('c', 'default-value')
这个简单的循环将得到你所需要的
for device_interface in device_config["GigabitEthernet"]:
print('GigabitEthernet')
print(*device_interface.items(), sep='\n')
https://www.geeksforgeeks.org/python-dictionary-items-method/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.