简体   繁体   中英

Not able to access nested Python dictionary with list inside of it

I have the nested dictionary below:

facter_networking: {"domain": "mylab.com", "fqdn": "mylab.com", "hostname": "mylab", "interfaces": {"ens192": {"bindings": [{"address": "20.9.8.1", "netmask": "255.255.255.221", "network": "20.33.50.62"}], "ip": "20.67.83.48", "mac": "00:00:06:0:e0:d6", "mtu": 1500, "netmask": "255.255.255.224", "network": "20.67.83.48"}, "ens224": {"bindings": [{"address": "20.67.83.48", "netmask": "20.67.83.48", "network": "20.67.83.48"}], "ip": "20.67.83.48", "mac": "20.67.83.48", "mtu": 1500, "netmask": "255.255.255.224", "network": "20.67.83.48"}, "lo": {"bindings": [{"address": "127.0.0.1", "netmask": "255.0.0.0", "network": "127.0.0.0"}], "ip": "127.0.0.1", "mtu": 65536, "netmask": "255.0.0.0", "network": "127.0.0.0"}}, "ip": "20.67.83.48", "mac": "00:00:00:9d:8f:d7", "mtu": 1500, "netmask": "255.255.255.224", "network": "20.67.83.48", "primary": "ens224"}

I can read mostly of the Keys values, but some of them I cannot.

for example:

print(myfile['facter_networking']['domain']) - it works.
print(myfile['facter_networking']['fqdn']) - it works.
print(myfile['facter_networking']['hostname'])    - it works.

However if I do.

print(myfile['facter_networking']['interfaces']['ens192']['bindings']['address']) - It doens't work.

On the other hand if I do:

print(myfile['facter_networking']['interfaces']

I get as result:

{"ens192": {"bindings": [{"address": "20.9.8.1", "netmask": "255.255.255.221", "network": "20.33.50.62"}], "ip": "20.67.83.48", "mac": "00:00:06:0:e0:d6", "mtu": 1500, "netmask": "255.255.255.224", "network": "20.67.83.48"}, "ens224": {"bindings": [{"address": "20.67.83.48", "netmask": "20.67.83.48", "network": "20.67.83.48"}], "ip": "20.67.83.48", "mac": "20.67.83.48", "mtu": 1500, "netmask": "255.255.255.224", "network": "20.67.83.48"}, "lo": {"bindings": [{"address": "127.0.0.1", "netmask": "255.0.0.0", "network": "127.0.0.0"}], "ip": "127.0.0.1", "mtu": 65536, "netmask": "255.0.0.0", "network": "127.0.0.0"}}, "ip": "20.67.83.48", "mac": "00:00:00:9d:8f:d7", "mtu": 1500, "netmask": "255.255.255.224", "network": "20.67.83.48", "primary": "ens224"}

Any idea how can I access these values above?

Quick fix: Your problem lies with your access to the last nested dictionary, "address". Since it is nested in a list before being a dictionary, you should get it like so:

myfile['facter_networking']['interfaces']['ens192']['bindings'][insert desired index here]['address']

Much better fix : However, you currently have a huge design problem. Nesting dicts in dicts in lists in dicts in dicts is a nesting hell, it prevents you from coding efficiently by making it much harder to debug whatever is happening. Luckily, python is an object oriented language, You can replace most of your dictionaries with dedicated objects, effectively managing your code better. and making it less likely for future bugs like this one to occur.

As said in the comment section, {"bindings": [{"address" is a list. And as always if you try to access an empty list using an index, it will throw exception. Instead loop through it, so that you will get empty list as result in case the list is empty

In [66]: [i["address"] for i in myfile['facter_networking']['interfaces']['ens192']['bindings']]
Out[66]: ['20.9.8.1']

The problem is that you try to access a list like a dict:

...{"bindings": [{"address": "20.9.8.1...

To acces the address you need

....["bindings"][0]["address"]
d = {
    'facter_networking': {
        "domain": "mylab.com",
        "fqdn": "mylab.com",
        "hostname": "mylab", 
        "interfaces": {
                        "ens192": 
                                {
                                "bindings": [{"address": "20.9.8.1", "netmask": "255.255.255.221", "network": "20.33.50.62"}],
                                "ip": "20.67.83.48", "mac": "00:00:06:0:e0:d6", "mtu": 1500, "netmask": "255.255.255.224", "network": "20.67.83.48"}, 
                                "ens224": {"bindings": [{"address": "20.67.83.48", "netmask": "20.67.83.48", "network": "20.67.83.48"}], 
                                "ip": "20.67.83.48", "mac": "20.67.83.48", "mtu": 1500, "netmask": "255.255.255.224", "network": "20.67.83.48"}, 
                                "lo": {"bindings": [{"address": "127.0.0.1", "netmask": "255.0.0.0", "network": "127.0.0.0"}],
                                "ip": "127.0.0.1", "mtu": 65536, "netmask": "255.0.0.0", "network": "127.0.0.0"}},
        "ip": "20.67.83.48", "mac": "00:00:00:9d:8f:d7",
        "mtu": 1500, "netmask": "255.255.255.224", "network": "20.67.83.48", "primary": "ens224"
    }
}

print(d['facter_networking']['interfaces']['ens192']['bindings'][0]['address'])

Output:

20.9.8.1

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM