Openpyxl & Python : Column Of Keys, Column Of Values - How To Add Up The Values And Assign Totals To Corresponding Keys
Apologies for the false start. I have now read the FAQs and hope my question meets the standards:). I have the following in a spreadsheet : Col1 Col2 1234 12.5 1234
Solution 1:
Try this code:
# Define a dict{} for key:value pairs
ref_total = {}
# Get Data from all rows
for row in ws.rows:
# Slice cells A,B from row tuple
cell_A = row[:1][0]
cell_B = row[1:2][0]
reference = cell_A.value
if reference in ref_total.keys():
ref_total[reference] += cell_B.value
else:
ref_total[reference] = cell_B.value
for key in sorted(ref_total.keys()):
print('%s %s' % (key, ref_total[key]))
Tested with Python:3.4.2 - openpyxl:2.4.1 - LibreOffice: 4.3.3.2
Post a Comment for "Openpyxl & Python : Column Of Keys, Column Of Values - How To Add Up The Values And Assign Totals To Corresponding Keys"