Python 类型错误:';浮动';对象没有属性'__获取项目';

Python 类型错误:';浮动';对象没有属性'__获取项目';,python,Python,这段代码是将csv文件读入两个列表的开始尝试。我得到下面的错误,我不明白为什么没有返回浮点。谢谢你的帮助 File "main.py", line 32, in <module> LR.openfile('djia_temp.csv') File "main.py", line 9, in openfile self.xs = self.tempDiff(dataAvgandtemp) File "main.py", line 18, in tem

这段代码是将csv文件读入两个列表的开始尝试。我得到下面的错误,我不明白为什么没有返回浮点。谢谢你的帮助

 File "main.py", line 32, in <module>
     LR.openfile('djia_temp.csv')  
 File "main.py", line 9, in openfile
     self.xs = self.tempDiff(dataAvgandtemp)   
 File "main.py", line 18, in tempDiff
     tdArray.append([vector[0]-vector[1]]) 
 TypeError: 'float' object has no attribute '__getitem__'
liner()
声明返回向量列表。事实并非如此。您正在列出
float
s:

vectors.append(float(tok[num]))
因此,当调用带有结果的
tempDiff()
时,
vector
是一个
float
,因此
vector[0]
抛出一个异常

我认为这就是它应该做的:将每个浮点添加到当前向量,然后将向量添加到结果:

@staticmethod
def liner(rows, columns, delimiter):
    vectors = []
    for row in rows:
        vector = []
        tok = row.split(delimiter)
        for num in columns:
            vector.append(float(tok[num])) # append to vector, not vectors
        vectors.append(vector)             # then append the vector to the result
    return vectors

作为旁注,您知道Python标准库中的
csv
模块吗?由于您正在导入
numpy
,可能您可以直接使用
np.loadtxt
np.genfromtxt
。或者使用
pandas
中非常快速的csv加载器。酷,我不知道numpy能解析得这么好。我来试试。谢谢你的回复!抱歉,这些变量的名称不正确。这个程序没有向量类。如果我将变量更改为:,我仍然得到相同的结果error@zeemy23:查看我的编辑。您应该将每个浮点附加到当前的
,然后将
附加到
@staticmethod
def liner(rows, columns, delimiter):
    vectors = []
    for row in rows:
        vector = []
        tok = row.split(delimiter)
        for num in columns:
            vector.append(float(tok[num])) # append to vector, not vectors
        vectors.append(vector)             # then append the vector to the result
    return vectors