该
csv模块一个接一个地循环遍历您的行,无需循环遍历该列。只是总和
int(row[1]):
with open("file.csv") as fin: headerline = fin.next() total = 0 for row in csv.reader(fin): total += int(row[1]) print total您可以将快捷方式与生成器表达式和
sum()内置函数一起使用:
with open("file.csv") as fin: fin.next() total = sum(int(r[1]) for r in csv.reader(fin))请注意,在Python中,字符串也是序列,因此在执行操作时,
for col inrow[1]:您将遍历
row[1];的各个字符。所以对于您的第一行将是
1和
2:
>>> for c in '123':... print repr(c)...'1''2''3'



