4

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
7,"Wage Payments",1000

I wish to add up all of the numbers in the 'value' column. So far, my code is this:

cr = csv.reader(open("file.csv","rb"))

for row in cr:    
    print row

#print sum(Value)

How could I go about summing that value?

Thank you.

3
  • sum(int(row[2]) for row in csv.reader(open("file.csv","rb")) if row[2].isdigit()) Commented May 18, 2012 at 18:38
  • 1
    sum(map(int, zip(*csv.reader(open("file.csv","rb")))[2][1:])) Commented May 18, 2012 at 18:42
  • 1
    sum(int(row['Value']) for row in csv.DictReader(open("file.csv","rb"))) Commented May 18, 2012 at 18:52

4 Answers 4

11

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
Sign up to request clarification or add additional context in comments.

1 Comment

Hint for any novices, remove the cr.next() line if you don't want to skip the first row in the csv file. That's just there to skip the header row.
3
cr = csv.reader(open("file.csv","rb"))
cr.next() # to skip the header 

total = 0
for row in cr:  
   total += int(row[2])
   # possibly do other things with data/rows 

print total

There are more terse ways (e.g., list comprehension, or generator expressions) to do this, but this provides the basic idea and also lets you do other things with each line as needed (as pointed out by @MarkRansom too)

6 Comments

If there's a need to do work (such as print) for every row in addition to generating the sum, this is the best way. Don't forget to cast to int though.
@MarkRansom thanks - I'll update the answer to include the cast. Do you know if CVS also pulls in the header?
Yes it does, it doesn't know or assume that there's a header on the file.
@Levon row[2] must be converted to int()
@Levon in python 2.7 the cr[1:] throws an error --> TypeError: '_csv.reader' object is not subscriptable.
|
0

Make a garbage value and read in the first 2 values and "trash" them then when reading in the 3rd value add it to your sum. Don't forget that initially you need to ignore the first 3 values of the file since it will pick up the headers.

Comments

0

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])

1 Comment

No need to make that list, you can just do sum(row[2] for row in cr)(although it looks like you will also need a cast to int)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.