Add all values in a CSV column in Python

后端 未结 4 1259
不知归路
不知归路 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条回答
  •  猫巷女王i
    2021-01-19 23:47

    Considering the first line of csv file is 'Day,Event,Value', you can use a generator expression with sum()

    >>> cr = csv.reader(open("file.csv","rb"))
    >>> cr.next()
    >>> print sum(int(x[2]) for x in cr)
    1500
    

提交回复
热议问题