为什么我得到字典键错误

时间:2020-12-29 07:37:15

标签: python-3.x

我正在读取一个带有嵌套字典的 JSON 文件,当获取顶层树的值时,数据如下所示。我需要迭代到下面的数据并获取每个元素的信息,如


{
   "access-policy":[
      {
         "name":"Allow_Redundant_Device",
         "match":{
            "source":{
               "zone":{
                  "zone-list":[
                     "Intf-Internet1-Failover-Zone",
                     "Intf-Internet1-Zone"
                  ]
               },
               "user":{
                  "local-database":{
                     "status":"disabled"
                  },
                  "external-database":{
                     "status":"disabled"
                  },
                  "user-type":"any"
               }
            },
            "destination":{
               "zone":{
                  "zone-list":[
                     "Intf-Internet1-Failover-Zone",
                     "Intf-Internet1-Zone"
                  ]
               }
            }
         },
         "set":{
            "action":"allow",
            "lef":{
               "options":{
                  "send-pcap-data":{
                     "enable":false
                  }
               }
            }
         }
      },
      {
         "name":"Allow_From_Hostbound",
         "match":{
            "source":{
               "zone":{
                  "zone-list":[
                     "HOST-GLOBAL-Zone"
                  ]
               }
            },
            "destination":{
               "zone":{
                  "zone-list":[
                     "Intf-Global-Zone",
                     "ptvi"
                  ]
               }
            }
         },
         "set":{
            "action":"allow"
         }
      },
      {
         "name":"Network_VPN_WEB_TEST",
         "match":{
            "source":{
               "zone":{
                  "zone-list":[
                     "Intf-GLOBAL-Zone"
                  ]
               },
               "address":{
                  "address-list":[
                     "VPN12",
                     "1VPN"
                  ],
                  "address-group-list":[
                     "VPN1"
                  ]
               },
               "user":{
                  "local-database":{
                     "status":"disabled"
                  },
                  "external-database":{
                     "status":"disabled"
                  },
                  "user-type":"any"
               }
            },
            "destination":{
               "zone":{
                  "zone-list":[
                     "L-ST-GLOBAL-Internet1",
                     "L-ST-GLOBAL-Internet2"
                  ]
               }
            },
            "services":{
               "predefined-services-list":[
                  "http",
                  "https"
               ]
            }
         },
         "set":{
            "security-profile":{
               "ips":{
                  "predefined-ips-profile":"Versa Recommended Profile",
                  "predefined-ips-profile-override":"Predefined-alert-IDS"
               },
               "url-filtering":{
                  "user-defined":"EMEA_NETWORK_VPN_WEB"
               }
            },
            "action":"allow",
            "synced-flow":"allow",
            "tcp-session-keepalive":"disabled",
            "lef":{
               "profile":"Default-Logging-Profile",
               "event":"both",
               "options":{
                  "send-pcap-data":{
                     "enable":false
                  }
               }
            }
         }
      },
      {
         "name":"GLOBAL-TO-SDWAN",
         "number":1,
         "match":{
            "source":{
               "zone":{
                  "zone-list":[
                     "Intf-GLOBAL-Zone"
                  ]
               },
               "user":{
                  "local-database":{
                     "status":"disabled"
                  },
                  "external-database":{
                     "status":"disabled"
                  },
                  "user-type":"any"
               }
            },
            "destination":{
               "zone":{
                  "zone-list":[
                     "ptvi"
                  ]
               }
            }
         },
         "set":{
            "action":"allow",
            "synced-flow":"allow",
            "lef":{
               "event":"never",
               "options":{
                  "send-pcap-data":{
                     "enable":false
                  }
               }
            }
         }
      },
      {
         "name":"Allow_From_SDWAN",
         "number":2,
         "match":{
            "source":{
               "zone":{
                  "zone-list":[
                     "ptvi"
                  ]
               },
               "user":{
                  "local-database":{
                     "status":"disabled"
                  },
                  "external-database":{
                     "status":"disabled"
                  },
                  "user-type":"any"
               }
            }
         },
         "set":{
            "action":"allow",
            "synced-flow":"allow",
            "lef":{
               "event":"never",
               "options":{
                  "send-pcap-data":{
                     "enable":false
                  }
               }
            }
         }
      },
      {
         "name":"DENY_MGMT_SERVERS",
         "description":"CHGxxxx",
         "match":{
            "source":{
               "zone":{
                  "zone-list":[
                     "Intf-GLOBAL-Zone"
                  ]
               },
               "address":{
                  "address-group-list":[
                     "AD_MGMT_SERVERS"
                  ]
               },
               "user":{
                  "local-database":{
                     "status":"disabled"
                  },
                  "external-database":{
                     "status":"disabled"
                  },
                  "user-type":"any"
               }
            },
            "destination":{
               "zone":{
                  "zone-list":[
                     "L-ST-GLOBAL-Internet1",
                     "L-ST-GLOBAL-Internet2"
                  ]
               }
            }
         },
         "set":{
            "action":"deny",
            "tcp-session-keepalive":"disabled",
            "lef":{
               "event":"never",
               "options":{
                  "send-pcap-data":{
                     "enable":false
                  }
               }
            }
         }
      },
      {
         "name":"DMZ-APPS-FILTERING",
         "match":{
            "source":{
               "zone":{
                  "zone-list":[
                     "Intf-GUEST-Zone"
                  ]
               },
               "address":{
                  "address-list":[
                     "1NETS",
                     "2NETS"
                  ],
                  "address-group-list":[
                     "1GUEST",
                     "2GROUP"
                  ]
               },
               "user":{
                  "local-database":{
                     "status":"disabled"
                  },
                  "external-database":{
                     "status":"disabled"
                  },
                  "user-type":"any"
               }
            },
            "destination":{
               "zone":{
                  "zone-list":[
                     "L-ST-GUEST-Internet1",
                     "L-ST-GUEST-Internet2"
                  ]
               }
            },
            "application":{
               "filter-list":[
                  "Malware_With_High_Risk"
               ]
            }
         },
         "set":{
            "action":"deny",
            "synced-flow":"allow",
            "tcp-session-keepalive":"disabled",
            "lef":{
               "event":"never",
               "options":{
                  "send-pcap-data":{
                     "enable":false
                  }
               }
            }
         }
      },
      {
         "name":"URL-FILTERING",
         "match":{
            "source":{
               "zone":{
                  "zone-list":[
                     "Intf-GUEST-Zone"
                  ]
               },
               "address":{
                  "address-list":[
                     "2-NETS",
                     "1-NETS"
                  ],
                  "address-group-list":[
                     "1GROUP",
                     "2GROUP"
                  ]
               },
               "user":{
                  "local-database":{
                     "status":"disabled"
                  },
                  "external-database":{
                     "status":"disabled"
                  },
                  "user-type":"any"
               }
            },
            "destination":{
               "zone":{
                  "zone-list":[
                     "L-ST-GUEST-Internet1",
                     "L-ST-GUEST-Internet2"
                  ]
               }
            },
            "url-category":{
               "predefined":[
                  "adult_and_pornography",
                  "confirmed_spam_sources",
                  "dead_sites",
                  "gross",
                  "hacking",
                  "malware_sites",
                  "nudity",
                  "open_http_proxies",
                  "peer_to_peer",
                  "phishing_and_other_frauds",
                  "proxy_avoid_and_anonymizers",
                  "spam_urls",
                  "spyware_and_adware"
               ]
            }
         },
         "set":{
            "action":"deny",
            "synced-flow":"allow",
            "tcp-session-keepalive":"disabled",
            "lef":{
               "event":"never",
               "options":{
                  "send-pcap-data":{
                     "enable":false
                  }
               }
            }
         }
      },
      {
         "name":"PUBLIC-DNS",
         "match":{
            "source":{
               "zone":{
                  "zone-list":[
                     "Intf-GUEST-Zone"
                  ]
               },
               "address":{
                  "address-list":[
                     "1-NETS",
                     "2-NETS"
                  ],
                  "address-group-list":[
                     "2-GUEST",
                     "3-GROUP"
                  ]
               },
               "user":{
                  "local-database":{
                     "status":"disabled"
                  },
                  "external-database":{
                     "status":"disabled"
                  },
                  "user-type":"any"
               }
            },
            "destination":{
               "zone":{
                  "zone-list":[
                     "L-ST-GUEST-Internet1",
                     "L-ST-GUEST-Internet2"
                  ]
               },
               "address":{
                  "address-list":[
                     "dns-google",
                     "dns-google1"
                  ]
               }
            },
            "services":{
               "services-list":[
                  "dns"
               ]
            }
         },
         "set":{
            "action":"allow",
            "synced-flow":"allow",
            "tcp-session-keepalive":"disabled",
            "lef":{
               "event":"never",
               "options":{
                  "send-pcap-data":{
                     "enable":false
                  }
               }
            }
         }
      }
   ]
}

从上面获取我在key=source的键值下面提取的键源和目的地的信息。

当我尝试获取数据时,例如

data_src = {'zone': 
               {'zone-list': ['Intf-GLOBAL-Zone']
               }, 
                'address': 
                   {'address-list': ['Netowkr1', 'Netowkr1'], 'address-group-list': ['SOMEDATA']
                   }, 
                    'user': 
                       {'local-database': {'status': 'disabled'}, 
                        'external-database': {'status': 'disabled'}, 
                        'user-type': 'any'
                       }
                    }

data_src['zone']['zone-list'] --> 这给了我正确的区域列表

data_src['address']['address-list'] 这没有给我任何键值并抛出

address_list = policy_source['address']['address-list']

密钥错误:'地址'

我几乎被困在这里一天了。你能指导我如何纠正这个问题。

我有一个巨大的文件,我需要将以下字段的数据提取到 json 文件中的每个行数据的 excel 中。

对于每行迭代,我需要提取以下数据

来源外:关键

'姓名' '描述' 区域列表' 地址:

来源外:关键 '名称' '描述' 区域列表' 地址

然后是剩下的数据

'服务' '放' '行动' '左'

如果还有什么需要,请告诉我。

现在在为此定义一个函数模式之前,我正在尝试阅读每个字典以制定代码

import json
import csv

with open("rule.json") as access_json:
    read_content = json.load(access_json)
    read_index = read_content['access-policy']

    for policy_data in read_index:

        policy_name = policy_data['name']
        policy_elements = policy_data['match']

        policy_source = policy_data['match']['source']

        print(policy_source)

        print(policy_source['zone']['zone-list'])  #This works fine

        #below doesnt work as this should be the next key value
        print(policy_source['address']['address-list'])



1 个答案:

答案 0 :(得分:0)

我的理解是您想要“地址列表”。您必须在嵌套的 json 中多次解析。 s 中的 loads 代表字符串,所以解析那么多时间直到你得到它。你可以用这个:

x = json.dumps(data_src)
t = json.dumps(json.loads(x).get("address","{}"))
json.loads(t).get("address-list")

enter image description here

相关问题