在python中求和一个csv列 [英] Sum a csv column in python

查看:25
本文介绍了在python中求和一个csv列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试对 csv 文件中的一列求和.该文件如下所示:

I'm trying to make a sum of a column in a csv file. The file looks like:

Date  Value
2012-11-20  12
2012-11-21  10
2012-11-22  3

这可以在数百行的范围内.我需要将总价值(在本例中为 25)打印到终端上.到目前为止,我有一些代码,但它导致的数字比它应该总和要小得多.在对其进行故障排除时,我打印了总和,并意识到它实际上不是将 12 + 10 + 3 相加,而是将每列中的数字和总和为 1 + 2 + 1 + 0 + 3,这显然等于很多较小的总数.这是我的代码,如果有人可以提出建议会很棒!

This can be in the range of hundreds of rows. I need to get the total of Value (in this case it would be 25) printed on to a terminal. I so far have some code but it's resulting in a much smaller figure than it should sum. When troubleshooting it, I did a print of the sum and realized that instead of summing 12 + 10 + 3, it actually breaks the numbers in each column and sums as 1 + 2 + 1 + 0 + 3, which obviously equals to a much smaller total. Here's my code, if someone could make a recommendation would be great!

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

推荐答案

csv 模块会逐行循环,无需再循环遍历列.只求和 int(row[1]):

The csv module loops over your rows one by one, there is no need to then loop over the column. Just sum 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() 内置函数的快捷方式:

You can use a shortcut with a generator expression and the sum() built-in function:

with open("file.csv") as fin:
    fin.next()
    total = sum(int(r[1]) for r in csv.reader(fin))

请注意,在 Python 中,字符串也是序列,因此当您执行 for col in row[1]: 时,您将遍历 row[1];所以对于你的第一行是 12:

Note that in Python, strings are sequences too, so when you do for col in row[1]: you are looping over the individual characters of row[1]; so for your first row that'd be 1 and 2:

>>> for c in '123':
...     print repr(c)
...
'1'
'2'
'3'

这篇关于在python中求和一个csv列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆