使用float错误将字符串转换为整数

时间:2014-07-03 08:41:36

标签: python list csv

我尝试在Python 3上导入CSV然后将其附加并浮动它,以便我可以使用它来进行一些计算。

import csv
data=[]
file=input ("Enter file name: ")
with open(file,"r") as f:
    reader=csv.reader(f)
    for row in reader:
        data.append(row) #creating list of lists
print (data)

print("Calculate COV")
lst = [val for subl in data for val in subl] #converting to list
float(lst) #error
print (lst)

def list_var(lst):
    var = 0
    avg = sum(lst)/len(lst)
    for i in lst:
        var += (avg - i)**2
    return
SD=list_var(lst)
print (SD)

这就是我得到的:

 TypeError: float() argument must be a string or a number

2 个答案:

答案 0 :(得分:4)

您尝试转换list to float。试试

lst = [float(val) for subl in data for val in subl if val.strip()]    

我认为你的列表理解应该是这样的

lst= [[float(val) for val in subl if val.strip()] for subl in data ]

如果您的列表如下所示,并且列表中的列表会尝试下面的代码

lst=[['13.25', '12.97', '13.12', '13.47', '13.44', '13.09', '12.86', '12.78', '12.91', '12.93', '12.91', '13.11'],
     ['12.92', '13.42', '13.58', '13.7', '13.62', '13.7', '13.31', '12.86', '12.59', '12.81', '13.46', '12.9'],
     ['13.39', '13.5', '13.29', '13.26', '13.38', '13.45', '13.46', '11.95', '', '12.57', '13.22', '12.88'], 
     ['12.48', '13.76', '13.7', '13.77', '13.08', '13.48', '13.25', '12.31', '12.56', '12.56', '12.95', '13.38'], 
     ['12.52', '14.07', '14.46', '14.13', '13.98', '14.07', '13.92', '12.7', '13.01', '12.79', '13', '13.13']]

然后尝试

from itertools import chain
newlst=[ float(val) for val in chain.from_iterable(lst) if val.strip()]

这种方法不是很好的编码实践,但有帮助

newlst=[]
for val in chain.from_iterable(lst):#
    if val.strip():
        try:
            newlst.append(float(val))
        except ValueError:
            # here you print the val . 
            #and check what type of val is coming here and perform logic 
            #so that you can convert these type also into float
            for i in val.split(';'):
                newlst.append(float(i if i.strip() else 0))

            pass

whitout itertools

for subl in data:
    for val in subl:
        if val.strip():
            try:
                newlst.append(float(val))
            except ValueError:
                # here you print the val . 
                #and check what type of val is coming here and perform logic 
                #so that you can convert these type also into float
                for i in val.split(';'):
                    newlst.append(float(i if i.strip() else 0))

                pass

答案 1 :(得分:1)

flattened = [float(x) for y in data for x in y if x]
print flattened
[13.25, 12.97, 13.12, 13.47, 13.44, 13.09, 12.86, 12.78, 12.91, 12.93, 12.91, 13.11, 12.92, 13.42, 13.58, 13.7, 13.62, 13.7, 13.31, 12.86, 12.59, 12.81, 13.46, 12.9, 13.39, 13.5, 13.29, 13.26, 13.38, 13.45, 13.46, 11.95, 12.57, 13.22, 12.88, 12.48, 13.76, 13.7, 13.77, 13.08, 13.48, 13.25, 12.31, 12.56, 12.56, 12.95, 13.38, 12.52, 14.07, 14.46, 14.13, 13.98, 14.07, 13.92, 12.7, 13.01, 12.79, 13.0, 13.13]

如果要清除输出,请在追加数据之前执行此操作。

with open(file,"r") as f:
    reader=csv.reader(f)
    for row in reader:
        data.append(row)

在你的循环中放置print row以确切了解你的数据是什么样的,然后移除你需要的任何东西,使它有效,以便作为浮点数进行投射。

数据的位置如下:

lst= [['1;2;3;4;5'], ['65;5;64;65;2'], ['215;5;85;65;54']]

spl= [x.split(";") for y in lst for x in y]
flattened = [float(x) for y in spl for x in y if x]
print flattened
[1.0, 2.0, 3.0, 4.0, 5.0, 65.0, 5.0, 64.0, 65.0, 2.0, 215.0, 5.0, 85.0, 65.0, 54.0]