Saturday, August 8, 2015

Excel to list of dictionaries in Python

This is a pretty neat python snippet I wrote to read an Excel (xlsx) file and convert its data into a list of dictionaries:

Note: you need to install the openpyxl module before you can use the snippet

$ sudo pip install openpyxl

So, you can use the snippet as following:

>> from excel_utils import excel_to_dict
>> data = excel_to_dict('/path/to/excel/file.xlsx', ['col1', 'col2', 'col3'])
>> data[0]

{'col1': u'Genius',
 'col2': u'Super',
 'col3': u'Awesome'}

Cool huh?