May-05-2018, 03:06 PM
greetings all,
I have a file given to me that is in YAML format
I have built out the part that gets the list into python dictionary:
here is where I get totally lost and would appreciate some pointers or assistance
I want to loop through each of the entries and if the department is FIN then I want it to put the devices in that entry into a list that I will be able to process through. The file is a list of routers in different locations. So the output I would be looking to be in the new list would be:
router1 router2 router17 router13 router14 router15 router16
Not very well versed in dictionaries and lists in dictionaries and all the searches I have found have confused me for some reason.
Thanks in Advance
Wally
I have a file given to me that is in YAML format
Quote:---
device_list:
location1:
department: [FIN]
description: location1
devices: [router1, router2]
location2:
department: [FIN]
description: location2
devices: [router3, router4, router5]
pwgroup: admin
location3:
department: [ACC]
description: location3
devices: [router6]
location4:
department: [ACC]
description: location4
devices: [router7, router8, router9]
location5:
pwgroup: admin
department: [ADM]
description: location5
devices: [router10, router11, router12]
location6:
department: [FIN]
pwgroup: admin
description: location6
devices: [router13, router14, router15, router16]
location2:
department: [FIN]
description: location7
devices: [router17]
pwgroup: admin
I have built out the part that gets the list into python dictionary:
import yaml with open('router.cfg', 'r') as lb: try: test = (yaml.load(lb)) except yaml.YAMLError as exc: print(exc) maindict = test['device_list'] for key, value in maindict.items(): print(key, value)the above returns the following:
Quote:location1 {'department': ['FIN'], 'description': 'location1', 'devices': ['router1', 'router2']}
location2 {'department': ['FIN'], 'description': 'location7', 'devices': ['router17'], 'pwgroup': 'admin'}
location3 {'department': ['ACC'], 'description': 'location3', 'devices': ['router6']}
location4 {'department': ['ACC'], 'description': 'location4', 'devices': ['router7', 'router8', 'router9']}
location5 {'pwgroup': 'admin', 'department': ['ADM'], 'description': 'location5', 'devices': ['router10', 'router11', 'router12']}
location6 {'department': ['FIN'], 'pwgroup': 'admin', 'description': 'location6', 'devices': ['router13', 'router14','router15', 'router16']}
here is where I get totally lost and would appreciate some pointers or assistance
I want to loop through each of the entries and if the department is FIN then I want it to put the devices in that entry into a list that I will be able to process through. The file is a list of routers in different locations. So the output I would be looking to be in the new list would be:
router1 router2 router17 router13 router14 router15 router16
Not very well versed in dictionaries and lists in dictionaries and all the searches I have found have confused me for some reason.
Thanks in Advance
Wally