Sum a csv column in python(在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]
;所以对于你的第一行是 1
和 2
:
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列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在python中求和一个csv列
基础教程推荐
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- 筛选NumPy数组 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01