此代码是开始尝试将 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__'
代码:
from processFile import processFile
import numpy as np
class processFile:
@staticmethod
def wholeFile(f):
fileArray = []
for line in f:
fileArray.append(line.strip())
return fileArray
@staticmethod
def liner(rows, columns, delimiter):
vectors = []
for row in rows:
vector = []
tok = row.split(delimiter)
for num in columns:
vectors.append(float(tok[num]))
return vectors
class linRegmain:
def openfile(self, file):
f = open(file)
a = processFile.wholeFile(f)[1:]
dataAvgandtemp = processFile.liner(a, [2,3], ";")
self.xs = self.tempDiff(dataAvgandtemp)
self.ys = processFile.liner(a,[1], ";")
print self.xs
print self.ys
def tempDiff(self, vectors):
tdArray = []
for vector in vectors:
tdArray.append([vector[0]-vector[1]])
return tdArray
if __name__ == '__main__':
LR = linRegmain()
LR.openfile('djia_temp.csv')