1

i need some enlightenment to create dictionary from the value returned. i only can see the last value in the dictionary.

The value of bgp_net

 network 10.29.92.76 mask 255.255.255.255
 network 10.29.92.77 mask 255.255.255.255
 network 10.29.92.83 mask 255.255.255.255
 network 10.237.32.0 mask 255.255.255.224
 network 10.239.1.101 mask 255.255.255.255
bgp_network_adv = {}
bgp_network = confparse.find_all_children(r"^router bgp")
for network in bgp_network:
    if 'network' in network:
        bgp_net= network.strip().split(' ')[1:4:2]
        bgp_network_adv = {'network':([a for a in bgp_net])}
        print (bgp_network_adv)

Ouput

   {'network': ['10.239.1.101', '255.255.255.255']}

i am expecting to get the rest of the value stored in the dictionary.

Thanks in advance

2 Answers 2

1

You need to have unique key for each key/value pair. In your code, you are setting key to network for all pairs. So, it is getting overwritten. You should look for another model to store data. One way can be to append a counter to your key. For example:

network0
network1
network2
...
Sign up to request clarification or add additional context in comments.

Comments

0

Use dict.setdefault with list.extend

Ex:

for network in bgp_network:
    if 'network' in network:
        bgp_net= network.strip().split()[1:4:2]
        bgp_network_adv.setdefault('network',[]).extend(bgp_net)
        print (bgp_network_adv)

Comments

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.