0

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 group Switch 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)
2
  • What research have you done? Do you know how to read the Excel file? Commented Apr 9, 2020 at 6:49
  • I have started using xlrd to open the excel file and looping thru tabs. Just not getting data correctly stored, let me update the code I have tried. Commented Apr 9, 2020 at 7:02

1 Answer 1

1

Use DataFrame.groupby with lambda function:

df = pd.read_excel(excelFile)

d = df.set_index('Switch A').groupby(level=0).apply(lambda x: x.to_dict('r')).to_dict()

print (d)

{
    'A18-SW-01': [{
        'swportA': 'Et17/1',
        'swipA': '192.168.128.1',
        'toswname': 'A20-FAB-01',
        'toswport': 'Et1/1',
        'toswip': '192.168.128.0'
    }, {
        'swportA': 'Et18/1',
        'swipA': '192.168.128.3',
        'toswname': 'A20-FAB-01',
        'toswport': 'Et2/1',
        'toswip': '192.168.128.2'
    }, {
        'swportA': 'Et19/1',
        'swipA': '192.168.128.5',
        'toswname': 'A20-FAB-01',
        'toswport': 'Et3/1',
        'toswip': '192.168.128.4'
    }],
    'A19-SW-01': [{
        'swportA': 'Et17/1',
        'swipA': '192.168.128.129',
        'toswname': 'A20-FAB-01',
        'toswport': 'Et4/1',
        'toswip': '192.168.128.130'
    }, {
        'swportA': 'Et18/1',
        'swipA': '192.168.128.131',
        'toswname': 'A20-FAB-01',
        'toswport': 'Et5/1',
        'toswip': '192.168.128.132'
    }, {
        'swportA': 'Et19/1',
        'swipA': '192.168.128.133',
        'toswname': 'A20-FAB-01',
        'toswport': 'Et6/1',
        'toswip': '192.168.128.134'
    }]
}
Sign up to request clarification or add additional context in comments.

15 Comments

thanks for providing quick answer, how to add the dict key, with the given name?
@miu - Isee. My solution working with DataFrame, so is necessary first step df = pd.read_excel(excelFile)
@miu - it shoud be one way. In pandas is used another way - reading all sheetnames to dictionary of DataFrame
@miu - I see it. There is space 'Switch A '
@miu - So change .set_index('Switch A') to .set_index('Switch A ') - space after A
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.