我正在尝试计算CSV文件中一列的总和。该文件如下:
Date Value
2012-11-20 12
2012-11-21 10
2012-11-22 3
这可能涉及几百行。我需要获得Value的总和(在此例中为25)并将其打印到终端上。我已经写了一些代码,但结果比应该相加的数字小得多。在排查问题时,我打印了总数,并意识到它不是对每列中的12 + 10 + 3进行求和,而是将每个数字分解然后相加,即1 + 2 + 1 + 0 + 3,显然得到的总和要小得多。这是我的代码,如果有人能提出建议就好了!
with open("file.csv")) as fin:
headerline = fin.next()
total = 0
for row in csv.reader(fin):
print col # for troubleshooting
for col in row[1]:
total += int(col)
print total