字典键问题

2024-06-16 12:31:51 发布

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

我是Python新手,需要一些代码支持。我使用一些通用API来接收以JSON格式返回的有关网络设备的信息

这是我的密码

def device_config():
            init = login()
            vedge_api = os.path.join(vmanage_url, 'dataservice/system/device/vedge')
            get = token[vmanage_url].get(vedge_api, verify=False)
            device_inventory = json.loads(get.content)
            inventory_json = device_inventory['data']
            print(inventory_json)

我从inventory\u json获得以下输出(部分输出)

[ {'deviceType': 'vedge', 'serialNumber': 'xxxxx', 'ncsDeviceName': 'xxxxx', 'configStatusMessage': 'In Sync', 'templateApplyLog': ['[3-Jun-2019 9:56:47 UTC] Configuring device with feature template: name', '[3-Jun-2019 9:56:47 UTC] Generating configuration from template', '[3-Jun-2019 9:56:51 UTC] Checking and creating device in vManage', '[3-Jun-2019 9:56:54 UTC] Device is online', '[3-Jun-2019 9:56:54 UTC] Updating device configuration in vManage', '[3-Jun-2019 9:56:57 UTC] Pushing configuration to device', '[3-Jun-2019 9:57:04 UTC] Template successfully attached to device'], 'uuid': 'afd33cb1-0836-54ad-b24c-ed4af537b57f', 'managementSystemIP': '0.0.0.0', 'templateStatus': 'Success', 'chasisNumber': 'afd33cb1-0836-54ad-b24c-ed4af537b57f', 'configStatusMessageDetails': '', 'configOperationMode': 'vmanage', 'deviceModel': 'vedge-cloud', 'deviceState': 'READY', 'validity': 'valid', 'platformFamily': 'vedge-x86', 'vedgeCSRCommonName': 'vedge-afd33cb1-0836-54ad-b24c-ed4af537b57f-2.viptela.com', 'deviceIP': '10.10.1.61', 'personality': 'vedge', 'uploadSource': 'Smart Account', 'local-system-ip': '10.10.1.61', 'system-ip': '10.10.1.61', 'model_sku': 'None', 'site-id': '100', 'host-name': 'vEdge-Munich-2', 'version': '19.2.0', 'vbond': '10.49.251.137', 'vmanageConnectionState': 'connected', 'lastupdated': 1566708480876, 'reachability': 'reachable', 'uptime-date': 1565947260000, 'defaultVersion': '19.2.0', 'availableVersions': ['18.3.3', '18.3.4', '18.4.0', '18.4.1', '19.1.0'], 'template': 'vedge-test2', 'templateId': 'f919e01b-f2c0-4bda-a652-cfc438a3eec9', 'lifeCycleRequired': True, 'expirationDate': 'NA', 'hardwareCertSerialNumber': 'NA'}
    {'deviceType': 'vedge', 'serialNumber': '555ba967519589f212ed3949be96372c', 'uuid': 'ace7648d-b931-464e-450d-51ff8052458e', 'managementSystemIP': '0.0.0.0', 'chasisNumber': 'ace7648d-b931-464e-450d-51ff8052458e', 'configOperationMode': 'cli', 'deviceModel': 'vedge-cloud', 'deviceState': 'READY', 'validity': 'valid', 'vedgeCertificateState': 'bootstrapconfiggenerated', 'personality': 'vedge', 'uploadSource': 'Smart Account', 'lifeCycleRequired': True, 'expirationDate': 'NA', 'hardwareCertSerialNumber': 'NA'}]

这里的问题是,输出在一个包含多个字典的列表中,其中一些没有提供deviceIP的键值

for key in inventory_json:
    print(key['deviceIP'])

...         print(key['deviceIP'])
... 

    10.10.1.63
    10.10.1.60
    10.10.1.61
    10.10.1.62
    Traceback (most recent call last):
      File "<stdin>", line 2, in <module>
    KeyError: 'deviceIP'

所以我得到了10.10.1.63等的初始结果,然后在没有这个键的情况下点击dictionary

我尝试了一些真/假逻辑或if键['deviceIP']在逻辑中,但不适合我。你知道吗

你能给个建议吗?你知道吗

谢谢


Tags: injsongetdevicetemplatesystemconfigurationjun
2条回答

您可以使用以下方法检查密钥是否存在:

if 'deviceIP' in key:
      print(key['deviceIP'])

您可以先检查,然后打印,如下所示:

if 'deviceIP' in key:
      print(key['deviceIP'])

或者,您可以使用dictionaryget(key,default_value)方法:

print(key.get('deviceIP',"No deviceIP"))

相关问题 更多 >