我有一個包含100列的csv文件。我想計算第4列至n的總和。我可以爲單個列生成總和,但是當我嘗試爲所有列嘗試失敗時。以下是我迄今爲止彙總CSV文件中的特定列
import decimal
import numpy as np
import os as os
import csv as csv
import re as re
import sys
col=10
values=[]
with open('test.csv', 'r') as f:
reader = csv.reader(f)
headers = reader.next()
for line in reader:
#print line
line = [int(i) for i in line]
col_totals = [sum(result) for result in zip(*line)]
print col_totals
#values.append(int(line[col]))
#csum=sum(values)
#print csum
感謝,
你要計算4-10列的總和,對於每一行?或者你想要計算所有行的第4列和所有行的第5列的和? – inspectorG4dget 2014-10-29 19:39:40
你試過'減少',因爲它[這裏]解釋(https://docs.python.org/2/library/functions.html#reduce)。 – 2014-10-29 19:47:30
是的我想要計算所有行的第4列和所有行的第5列的總和等? – learningcurve 2014-10-29 21:02:09