Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 从dict中提取键值_Python_Python 3.x - Fatal编程技术网

Python 从dict中提取键值

Python 从dict中提取键值,python,python-3.x,Python,Python 3.x,感谢所有在这一点上帮助过我的人。我仍然需要一些额外的帮助。我从设备配置中提取了以下dict结构: {'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'},'messa

感谢所有在这一点上帮助过我的人。我仍然需要一些额外的帮助。我从设备配置中提取了以下dict结构:

{'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'},'网络:{'point-to-point':[None],'mop]}{'enabled':False,'sysid':False},'Cisco IOS XE以太网:协商':{'auto':True},{'name':'2','shutdown':[None],'mop':{'enabled':False,'sysid':False},'Cisco IOS XE以太网:协商':{'auto':True},{'name':'3','shutdown':[None],'mop':{'enabled':False,'sysid':False},'Cisco IOS XE以太网:协商':{'auto True}]}

我试图创建一个if语句来检测接口下是否存在并配置了“auth key”。我试图定义一个变量
ospf\u auth=interface\u type\u gig['ip']['Cisco-IOS-XE-ospf:router-ospf']['ospf']['message-digest-key'][0]
,但它在
['Cisco-IOS-XE-ospf:router-ospf']上给出了一个键错误
以下格式中的if语句存在相同错误

# import functions
from cisco_xe_api import *

# define variables
device_config = api_get_conf()

for interface_type_gig in device_config['Cisco-IOS-XE-native:native']['interface']['GigabitEthernet']:
    if 'message-digest-key' in interface_type_gig['ip']['Cisco-IOS-XE-ospf:router-ospf']['ospf']:
        ospf_auth = interface_type_gig['ip']['Cisco-IOS-XE-ospf:router-ospf']['ospf']['message-digest-key'][0]['md5']
        print(ospf_auth.keys)

我能解决这个问题

for interface_type_gig in device_config['Cisco-IOS-XE-native:native']['interface']['GigabitEthernet']:
    if 'ip' in interface_type_gig:
        if 'Cisco-IOS-XE-ospf:router-ospf' in interface_type_gig['ip']:
            ospf_presence = interface_type_gig['ip']['Cisco-IOS-XE-ospf:router-ospf']['ospf']
            #print(ospf_presence.keys())
            if 'message-digest-key' in ospf_presence:
                auth_presence = ospf_presence['message-digest-key'][0]['md5']
                #print(auth_presence.keys())
                if 'auth-key' in auth_presence:
                    print('MD5 Authentication Enabled!')

错误是什么?这是相当多的代码,请提供一个。另外,
import*
通常是不好的做法。我用你要求的代码更新了问题。谢谢!你能分享完整的
回溯吗?\u type\u gig['ip']['Cisco-IOS-XE-ospf:router ospf'['ospf']:KeyError:'Cisco IOS XE ospf:router-ospf'您需要在问题本身中提供一个。现在您缺少错误消息和
设备配置的定义。
。数据的结构是否实际相关?似乎您只对其中的一部分/级别有问题。