I have an Excel worksheet and I want to create a dict which has the cell values as a list with cell column being the key. Let say that spreadsheet has data that looks like,
A B C (columns)
1 2
3 4
5 f
I want a dict that looks like,
cbyc = {'A': [1,3,5]; 'B':[2,4,f]; 'C';[None, None, None]}
I am doing this with the following code
import openpyxl as oxl
wb = oxl.load_workbook('myxlworkbook.xlsx')
sheet = wb.get_sheet_by_name('Sheet1')
allcells = sheet.get_cell_collection()
cbyc = {}
for c in allcells:
if c.value is not None:
if c.column not in cbyc.keys():
cbyc[c.column] = [c.value]
else:
cbyc[c.column].append(c.value)
And this work,.. but I am sure there is a more efficient way to create this dict with the if.. else logic
is there a better way? Maybe there is something in the openpyxl that can give such a list