0
import csv

csvfile = open(r"C:\Users\Administrator\Downloads\canberra_2011_2012.csv")

header = csvfile.readline()  
csv_f = csv.reader(csvfile)   
for row in csv_f:
    first_value = float(row[5]) 
    total = sum(first_value)
    length = len(first_value)
    average = total/length
    print("average = ",average)   

When i run this code, it said

TypeError: 'float' object is not iterable

But when I change the line 7 to

first_value = [float(row[5]) for row in csv_f

then it works. This confuses me, can anyone help me?

5
  • What programming language is this? Commented Aug 13, 2014 at 13:33
  • 3
    What do you want sum(first_value) to produce? As it is, you're trying to sum the single value found in the 6th column of each row by itself. The indented code runs separately once for each row in your spreadsheet. Commented Aug 13, 2014 at 13:34
  • This is python, so how can i fix it? Commented Aug 13, 2014 at 13:38
  • 1
    Your list comprehension already did fix it, didn't it? Your sum and len both assume that first_value is a list of values, not a single value. Making it actually a list of values (probably with a better name) fixes it. Commented Aug 13, 2014 at 13:39
  • if I use first_value = [float(row[5]) for row in csv_f], it did fix it. but to find the average is just task one, in task two, it asked me to find the average value in both year 2011 and year 2012. the third column of this file is the year, so I plan to test when value in column 3 equals 2011, then output the value in column 6, but the output is "[]" without any value Commented Aug 13, 2014 at 13:44

3 Answers 3

1

The other answer is much more elegant than mine, but the following is closer to the spirit of your original code. It may make your errors more obvious. I apologize for the crappy formatting. I'm new to this site.

import csv
csvfile = open(r"C:\Users\Administrator\Downloads\canberra_2011_2012.csv")

header = csvfile.readline()  
csv_f = csv.reader(csvfile)
length = 0
total = 0.0   
for row in csv_f:
    first_value = float(row[5])
    total = total + first_value
    length += 1
if length > 0:
    average = total/length
    print("average = ",average)   
Sign up to request clarification or add additional context in comments.

Comments

0

I think you want to collect all the first_values and then do some calculations. To do that, you must step through each row of the csv file and first collect all the values, otherwise you are summing one value and that's the source of your error.

Try this version:

import csv


with open(r"C:\Users\Administrator\Downloads\canberra_2011_2012.csv") as f:
    reader = csv.reader(f)
    values = [float(line[5]) for line in reader]

# Now you can do your calculations:

total = sum(values)
length = len(values)
# etc.

Comments

0

You are getting this error at this line of your code,

total = sum(first_value)

The error is raised because sum is a function of iterable object. As in your code, the first_value is a float object. So you can not use sum function on it. But when you use list compression,

first_value = [float(row[5]) for row in csv_f]

then the first_value is a list type object consisting the float values of row[5]. So you can apply sum function on it without raising error.

Apart from list compression, you can also append the values in a list in your for loop and calculate the sum and length after the loop.

first_values = []
for row in csv_f:
    first_value = float(row[5])
    first_values.append(first_value)

total = sum(first_values)
length = len(first_values)
average = total/length    

Comments

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.