I have excel file with multiple tabs with this following column header and data. I would like to parse the data from the excel into the list of nested dict, so that I can build the configuration.
Like to create data output following format:
[
{
"SW-01": [{"swportA": "Et17/1", "swipA": "192.168.128.1", "toswname": "A20-01","toswport": "Et1/1", "toswip": "192.168.128.0"},
{"swportA": "Et18/1", "swipA": "192.168.128.3", "toswname": "A20-","toswport": "Et2/1", "toswip": "192.168.128.2"},
{"swportA": "Et19/1", "swipA": "192.168.128.5", "toswname": "A20-01", "toswport": "Et3/1", "toswip": "192.168.128.4"}]
},
{
"SW-02": [{"swportA": "Et17/1", "swipA": "192.168.128.129", "toswname": "A20-01", "toswport": "Et4/1", "toswip": "192.168.128.130"},
{"swportA": "Et18/1", "swipA": "192.168.128.131", "toswname": "A20-01", "toswport": "Et5/1", "toswip": "192.168.128.132"},
{"swportA": "Et19/1", "swipA": "192.168.128.133", "toswname": "A20-01", "toswport": "Et6/1", "toswip": "192.168.128.134"}]
}
]
CODE Tried:
book = xlrd.open_workbook(excelFile)
worksheet = book.sheet_by_index(0)
data = []
for sheet in book.sheets():
listofiles = []
for i in range(2, sheet.nrows):
sw = {}
row = sheet.row_values(i)
swname = row[0]
if not swname in data:
swname
data.append( { swname: {
'swport': row[1],
'swip': row[2],
'toswname': row[3],
'toswport': row[4],
'toswip': row[5]
}})
pprint(data)
but it keeps repeating the Switch column, with each entry.
Merging two DICTs into new Dict with groupSwitch A
Here is the code I tried
d = {}
for key in (*dic1, *dic2):
try:
d.setdefault(key,[]).append(dic1[key])
except KeyError:
pass
try:
d.setdefault(key,[]).append(dic2[key])
except KeyError:
pass
pprint(d)