I am trying to calculate the sum of all values from a csv file with the first column of a row containing a key. All this data is to be placed in a dictionary in Python.
I have come up with this code so far. The only problem is that not all values are integer, some are blank and contain strings. I need to update the code to ignore these.
An obvious improvement would be to calculate the amount of column the file has instead of assuming it has up to three columns of data, I'm not quite sure how to implement this though!
import csv
d = {}
with open(filename) as csvfile:
rdr = csv.reader(csvfile)
if header == True:
next(rdr, None)
for row in rdr:
d[row[0]] = int(row[1]) + int(row[2]) + int(row[3])
return d
I appreciate any help!