0

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?

2
  • 4
    {"bindings": [{"address":.. has a list as value, not another dictionary. So you need myfile['facter_networking']['interfaces']['ens192']['bindings'][0]['address'], assuming only one item. Commented Jul 25, 2020 at 16:09
  • I tried it before, but didn't work. get_network = myfile['facter_networking']['interfaces']['ens192']['bindings'][0]['address'] KeyError: 'ens192' Commented Jul 25, 2020 at 16:57

4 Answers 4

1

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']
Sign up to request clarification or add additional context in comments.

2 Comments

I didn't understand your loop. i["address"] for i in myfile['facter_networking']['interfaces']['ens192']['bindings']] I tried to reproduce here but didn't work. SyntaxError: invalid syntax Invalid Syntax something is missing, I didn't understand i["address"]
This is called list comprehension. It's same as normal for loop. In your code you are missing [ in the beginning. so it throws error.
1

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.

1 Comment

I tried before, this is the output. For some reason do not work, am I doing something wrong? myip = myfile['facter_networking']['interfaces']['ens192']['bindings'][0]['address'] KeyError: 'ens192'
0

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"]

1 Comment

I tried before, this is the output. For some reason do not work, am I doing something wrong? myip = myfile['facter_networking']['interfaces']['ens192']['bindings'][0]['address'] KeyError: 'ens192'
0
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

4 Comments

I manage to find my issue. So, as I'm reading a bunch of files, i was expecting all of them to have the same keys, but seems this is not the case for this particular Key. So, some files have ens192 others have bond0 and others have ens198, I only noticed now. Is there any way to put something like: '*' print(d['facter_networking']['interfaces'][*]['bindings'][0]['address'] Because here the only thing which differs from each file are the keys ens192, bond0 and ens198.
As a workaround I did try: except as below: try: get_network_inter = myfile['facter_networking']['interfaces']['bond0']['bindings'][0]['address'] print(files + '---> bond0: ' + get_network_inter) except: get_network_inter = myfile['facter_networking']['interfaces']['ens192']['bindings'][0]['address'] print(files + '---> ens192: ' + get_network_inter)
Oh, I'm discovering each time that there are more different names for this specific key. Any suggestion?
Ok, I managed to use some IF conditions (7 different). Thanks,

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.