I have this class definition, it is a datafile that will be read and a single column and sample rate information will be extracted.
class dataFile:
def __init__(self, oldname, folName):
self.name = oldname # Original File name
self.nm_split = os.path.splitext(oldname)
self.newName = str(self.nm_split[0] + "_trimmed" + self.nm_split[1])
self.path = "./" + folName ## Output Folder path
self.y = [] ## Data to be extracted into list
def readDat(self): ## Extract the voltage column and put into array
with open(self.name) as f:
for line in f:
if line.startswith('%'):
if datafile.lookUp in line:
self.sRate = float(str.split(line)[3])
else:
continue
elif not len(line.strip()) ==0:
row = line.split()
self.y.append(float(row[2]))
else:
continue
self.y = np.array(self.y);
return self.y, self.sRate
When I run the program, and specifically the readDat()
function, I get the following Message:
AttributeError: datafile instance has no attribute 'sRate'
Clearly it is defined within that first if statement in readDat
so why isn't the interpreter seeing it?
答案 0 :(得分:0)
One of the ifs before sRate is defined is not doing what you expect it to and sRate is never being declared.
I don't see a declaration for datfile.lookUp either:
for line in f:
if line.startswith('%'):
if datafile.lookUp in line: # <---- lookUp?
self.sRate = float(str.split(line)[3])
else:
continue
Consider declaring .sRate with a default value; it's not pythonic but it can avoid confusion.