gpt4 book ai didi

Python - 值错误 : invalid literal for float():

转载 作者:太空狗 更新时间:2023-10-29 11:05:05 25 4
gpt4 key购买 nike

我有一个 csv 文件,我正在尝试计算其中每一列的平均值。

#!/usr/bin/python

with open('/home/rnish/Desktop/lbm-reference.dat.ref-2013-01-30-13-00-15big.csv', "rU") as f:
columns = f.readline().strip().split(' ')
numRows = 0
sums = [0] * len(columns)

for line in f:
values = line.split(" ")
print values
for i in xrange(len(values)):
sums[i] += float(values[i])
numRows += 1

# for index, summedRowValue in enumerate(sums):
# print columns[index], 1.0 * summedRowValue / numRows

我得到的错误是:

  File "excel.py", line 15, in <module>
sums[i] += float(values[i])
ValueError: invalid literal for float(): 0,536880742,8861743,0,4184866,4448905

print values 的输出是这样的:

['0,256352728,10070198,5079543,5024472,34764\n']
['0,352618127,10102320,4987654,3082111,1902909\n']
['0,505838297,9977968,423278,4709666,5041639\n']
['0,506598469,10083489,0,5032146,5054715\n']
['0,536869414,7229488,39934,4322290,3607046\n']

这是 csv 文件的样子:

0,256641418,10669052,4803710,4759922,0
0,484517531,9889830,1457230,4084777,4959529
0,506902273,9673699,0,5281012,5293376

有人可以阐明并帮助我理解这个问题吗:

我在阅读了几篇文章后假设这是由于换行符引起的。我说得对吗?

最佳答案

您在空格处拆分 .cvs 文件 - 但字符串中没有空格。尝试以逗号分隔:

    columns = f.readline().strip().split(',')

关于Python - 值错误 : invalid literal for float():,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14618923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com