我试图从键'ip‘中提取一个特定的值,在这个例子中,它是192.168.200.200,但在某些情况下,它将是不同的,可能不止一个。我是刚接触python的人,有人能帮我拿出价值吗?
# import functions
from cisco_xe_api import *
# define variables
device_config = api_get_conf()
# Rule SV-105995r2_rule: The Cisco router must be configured to implement message
# authentication for all control plane protocols.
def sv105995r2rule_ospf():
#device_config = api_get_conf()
routing_protocol = device_config['Cisco-IOS-XE-native:native']['router']
ospf_networks = device_config['Cisco-IOS-XE-native:native']['router']['Cisco-IOS-XE-ospf:router-ospf']['ospf']
protocol_intf = device_config['Cisco-IOS-XE-native:native']['interface']
if 'Cisco-IOS-XE-ospf:router-ospf' in routing_protocol.keys():
print('\nOSPF is configured on this device. Checking for MD5 authentication.'
print(ospf_networks.items())以下是print语句的输出:
OSPF is configured on this device. Checking for MD5 authentication.
dict_items([('process-id', [{'id': 100, 'area': [{'area-id': 0, 'authentication': {'message-digest': [None]}}], 'network': [{'ip': '192.168.200.200', 'wildcard': '0.0.0.0', 'area': 0}]}])])谢谢你的帮忙!
发布于 2020-04-10 15:39:50
这个结构是dict→list→dict→list→dict,所以在您的示例中,您可以获得如下所示的IP:
ospf_networks['process-id'][0]['network'][0]['ip']我对您正在使用的API一无所知,但假设任何列表都可能有多个项,那么打印所有的IP应该如下所示:
for d0 in ospf_networks['process-id']:
for d1 in d0['network']:
print(d1['ip'])发布于 2020-04-10 15:22:22
对于您提供的示例,获取ip的代码如下:
ospf_networks['process_id'][0]['network'][0]['ip']事情是这样的:
得到一个值键'process_id'.
现在,要获得多个ip地址( ospf_networks dict ),完全取决于它们在结构中的位置。
https://stackoverflow.com/questions/61143195
复制相似问题