This code is a beginning attempt at reading a csv file into a couple of lists. I'm getting the error below, and I can't see why the float isn't being returned. Thanks for your help!
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__'
Code:
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')
liner()
claims to return a list of vectors. It does not. You're making a list of float
s:
vectors.append(float(tok[num]))
Therefore, when you call tempDiff()
with the result, vector
is a float
, so vector[0]
throws an exception.
I think this is what it's supposed to do: add each float to the current vector, then add the vector to the result:
@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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.