I have a text file with the following column names. I want these to be the keys in my dictionary, and grab the value data from separating lines in the txt file, which contains a series of lines like such:
VLAN1100 84:03:28:a8:b3:18 D - ge-0/0/45.0 0 0 10.68.8.189:
key_list = ['vlan name', 'mac address', 'mac flags', 'age', 'logical interface', 'nh index', 'rtr id', 'IP']
with open('prog-input.txt', 'r') as file:
lines = file.readlines()`
#separate column data into lines
for line in lines:
if 'VLAN' in line:
#skip headers
entry = line.split()
res = {}
for key in key_list:
for value in entry:
res[key] = value
entry.remove(value)
break
print(res)
file.close()
This is correctly splitting the data into key value pairs, but not assigning them to individual dictionaries.
OUTPUT:
{'vlan name': 'VLAN1100', 'mac address': '94:40:c9:3a:44:1a', 'mac flags': 'D', 'age': '-', 'logical interface': 'ge-0/0/16.0', 'nh index': '0', 'rtr id': '0', 'IP': '10.68.14.67'}
{'vlan name': 'VLAN1100', 'mac address': '94:40:c9:3a:91:b6', 'mac flags': 'D', 'age': '-', 'logical interface': 'ge-0/0/8.0', 'nh index': '0', 'rtr id': '0', 'IP': '10.68.14.59'}
{'vlan name': 'VLAN1100', 'mac address': '94:40:c9:3a:e5:b2', 'mac flags': 'D', 'age': '-', 'logical interface': 'ge-0/0/17.0', 'nh index': '0', 'rtr id': '0', 'IP': '10.68.14.68'}
{'vlan name': 'VLAN1100', 'mac address': 'f4:a7:39:9c:4c:e0', 'mac flags': 'D', 'age': '-', 'logical interface': 'ge-0/0/47.0', 'nh index': '0', 'rtr id': '0', 'IP': '10.68.8.191'}`
I only care about the key:value pairs of 0, 1, 4, 7. I would like to assign each line entry with an ID, possibly based on the 'mac address', which is unique.
I also tried using this code, but I don't understand how to use map, so I'd rather it be spelled out:
if 'VLAN' in line:
device = {k:v for k, *v in map(line.split, file)}
for key in key_list:
for value in device:
device.append()
print(device)
But it doesn't work. I want to be able to create a dictionary for each line item, and then put them inside a List, to query later.
data1 = {'vlan name': 'VLAN1100'....}
,data2 = {'vlan name': 'VLAN1100'....}
? I don't recommend that. Use a list. Ex:all_data = [dict(zip(key_list, line.split())) for line in lines]
.