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
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'}
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.
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.
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'}
(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)