类型错误:“float”对象不可迭代3。

时间:2022-01-05 20:23:05
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

但是当我把第7行改为

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

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

它的工作原理。这让我很困惑,有人能帮我吗?

3 个解决方案

#1


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)   

#2


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.

我认为您需要收集first_values然后进行一些计算。要做到这一点,您必须遍历csv文件的每一行并首先收集所有值,否则您将求和一个值,这就是错误的来源。

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.

#3


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,

因为sum是可迭代对象的函数,所以会产生错误。与您的代码一样,first_value是一个浮点对象。所以不能用求和函数。但是当你使用列表压缩时,

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.

然后first_value是一个列表类型对象,由第[5]行的浮点值组成。所以你可以对它应用求和函数而不引起误差。

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.

除了列表压缩之外,还可以在for循环中添加列表中的值,并计算循环之后的和和长度。

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    

#1


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)   

#2


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.

我认为您需要收集first_values然后进行一些计算。要做到这一点,您必须遍历csv文件的每一行并首先收集所有值,否则您将求和一个值,这就是错误的来源。

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.

#3


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,

因为sum是可迭代对象的函数,所以会产生错误。与您的代码一样,first_value是一个浮点对象。所以不能用求和函数。但是当你使用列表压缩时,

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.

然后first_value是一个列表类型对象,由第[5]行的浮点值组成。所以你可以对它应用求和函数而不引起误差。

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.

除了列表压缩之外,还可以在for循环中添加列表中的值,并计算循环之后的和和长度。

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