I have a two column csv file
row vote
1 0
1 0
1 1
2 0
2 0
3 1
3 0
I'm trying to write a python script so that each vote is counted depending on the row number, thus outputting
row vote
1 1
2 0
3 1
what I've tried so far with a text file:
from collections import defaultdict
d = defaultdict(int)
with open("data.txt") as f:
for line in f:
tokens = [t.strip() for t in line.split(",")]
try:
row = int(tokens[1])
vote = int(tokens[1])
except ValueError:
continue
d[row] += vote
print d
and I'm getting IndexError: list index out of range errors
row = int(tokens[0])?