Basically I'm trying to add together the count column values of item columns with the same name in a csv file. I then need to sort the results in ascending alphabetic order by item column values. For example:
Leading Cause, Deaths
Diabetes Mellitus, 123
Influenza and Pneumonia, 325
Diabetes Mellitus, 100
I need to add the values 123 and 100 to get a new row for Diabetes.
It should look like this:
Diabetes Mellitus, 223.
This is the code I have so far:
import csv
import sys
with open(sys.argv[1], 'r') as File:
reader = csv.reader(File)
itemindex = sys.argv[2]
countindex = sys.argv[3]
item column = 0
count column = 0
first row = True
dictionary = {}
for row in reader:
if firstrow == True:
firstrow = False
itemcolumn = row.index(itemindex)
countcolumn = row.index(countindex)
else:
if item column in dictionary:
# Add the item at the row's count column (converted to an int) to the
# prexisting entry with that item column.
else:
#create a new entry in the dictionary
print(itemindex + "," + countindex)
for key, value in sorted(dictionary)
print(key + "," + value)
The commented parts are the ones I'm stuck on.