Add all values in a CSV column in Python

后端 未结 4 1215
不知归路
不知归路 2021-01-19 23:11

These seems like something very simple, but search as I might I just can\'t get past it.

I have a CSV file like this:

Day,Event,Value
1,\"Rent\",500
         


        
4条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-19 23:55

    Given that 'value' is in column 2, you can perform the following simple for loop:

    value_sum=0
    for row in cr:
        value_sum += row[2]
    

    Or, you can use a comprehension, if you understand it:

    value_sum = sum([row[2] for row in cr])
    

提交回复
热议问题