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?
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.sumandlenboth assume thatfirst_valueis a list of values, not a single value. Making it actually a list of values (probably with a better name) fixes it.