Posts: 221
Threads: 71
Joined: Dec 2017
Feb-22-2019, 10:26 AM
(This post was last modified: Feb-22-2019, 10:27 AM by anna.)
Hi There,
bgp_prefix_dict = {}
#dict for BGP prefixes
for line in ip_prefix:
if 'ip ip-prefix' in line:
prefix_name = line.strip().split(' ')[2]
subnet = line.strip().split(' ')[6]+"/"+line.strip().split(' ')[7]
bgp_subnet = str(IPNetwork(subnet))
bgp_prefix_dict['prefix_name'] = prefix_name
bgp_prefix_dict['bgp_subnet'] = bgp_subnet
#print(bgp_prefix_dict) #### Able to print all key values
print(bgp_prefix_dict) ## here its printing last key and value
for (key,value) in bgp_prefix_dict.items():
print(key, "> ",value) Output: {'prefix_name': 'MORADABAD_INTERNET_59105832429', 'bgp_subnet': '103.217.78.0/23'}
in loop
Output: prefix_name > MORADABAD_INTERNET_59105832429
bgp_subnet > 103.217.78.0/23
Posts: 221
Threads: 71
Joined: Dec 2017
its not working in for loop, as per my understanding, update will replace key,value if already present.
same result
Output: {'prefix_name': 'MORADABAD_INTERNET_59105832429', 'bgp_subnet': '103.217.78.0/23'}
Posts: 1,950
Threads: 8
Joined: Jun 2018
Your options include at least following methods: check whether key exists and if not create key with value of empty list otherwise append; use .get method; use .setdefault method; use defaultdict.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy
Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Posts: 8,156
Threads: 160
Joined: Sep 2016
you are updating same keys over and over again, while iterating over the ip_prefix:
bgp_prefix_dict['prefix_name'] = prefix_name
bgp_prefix_dict['bgp_subnet'] = bgp_subnet bgp_prefix_dict will always have just two keys and their values will be coming from the current element from ip_prefix. At the end of the loop their values will come form last element in ip_prefix.
Posts: 221
Threads: 71
Joined: Dec 2017
Feb-22-2019, 12:51 PM
(This post was last modified: Feb-22-2019, 12:51 PM by anna.)
will check this, there was some issue while pasting code in last code.. some indent issue
Hi Buran,
I am able to find all key and values in loop, but its printing only last key value out of loop
bgp_prefix_dict = {}
#dict for BGP prefixes
for line in ip_prefix:
if 'ip ip-prefix' in line:
prefix_name = line.strip().split(' ')[2]
subnet = line.strip().split(' ')[6]+"/"+line.strip().split(' ')[7]
bgp_subnet = str(IPNetwork(subnet))
bgp_prefix_dict['prefix_name'] = prefix_name
bgp_prefix_dict['bgp_subnet'] = bgp_subnet
print(bgp_prefix_dict) #### Able to print all key values Output: {'prefix_name': 'Uzaina', 'bgp_subnet': '103.126.62.0/24'}
{'prefix_name': 'WI_LINK_NETWORK_12105830372', 'bgp_subnet': '103.82.100.0/22'}
{'prefix_name': 'WI_LINK_NETWORK_12105830372', 'bgp_subnet': '103.82.102.0/23'}
{'prefix_name': 'WI_LINK_NETWORK_12105830372', 'bgp_subnet': '103.82.101.0/24'}
{'prefix_name': 'WI_LINK_12105830371', 'bgp_subnet': '103.82.100.0/22'}
{'prefix_name': 'WI_LINK_12105830371', 'bgp_subnet': '103.82.102.0/23'}
{'prefix_name': 'WI_LINK_12105830371', 'bgp_subnet': '103.82.101.0/24'}
{'prefix_name': 'MORADABAD_INTERNET_59105832429', 'bgp_subnet': '103.217.78.0/23'}
Posts: 8,156
Threads: 160
Joined: Sep 2016
Feb-22-2019, 01:04 PM
(This post was last modified: Feb-22-2019, 01:05 PM by buran.)
(Feb-22-2019, 12:51 PM)anna Wrote: I am able to find all key and values in loop, but its printing only last key value out of loop Yes, because you overwrite the previous value. What you print proves exactly that!!!!
#list for BGP prefixes dicts
bgp_prefix_list = []
for line in ip_prefix:
if 'ip ip-prefix' in line:
#dict for BGP prefixes
bgp_prefix_dict = {}
prefix_name = line.strip().split(' ')[2]
subnet = line.strip().split(' ')[6]+"/"+line.strip().split(' ')[7]
bgp_subnet = str(IPNetwork(subnet))
bgp_prefix_dict['prefix_name'] = prefix_name
bgp_prefix_dict['bgp_subnet'] = bgp_subnet
print(bgp_prefix_dict) #### Able to print current dict
bgp_prefix_list.append(bgp_prefix_dict)
print(bgp_prefix_list)
Posts: 221
Threads: 71
Joined: Dec 2017
|