In a folder "data_last", there are three excel files thay show place, date, temperature, wind speed, humidity. using these data, i want to make each avg, std, max, min prgram by using class. But error....!!
File "C:/Users/whgoa/PycharmProjects/PythonStudy/assign_last.py", line 76, in <module>
weath[name] = Grade(open(data_path+name+'.csv','r'))
File "C:/Users/whgoa/PycharmProjects/PythonStudy/assign_last.py", line 22, in __init__
self.dt1 = self.dt1.append(self.dt[2])[1:]
TypeError: 'NoneType' object is not subscriptable
and i think there are a lot of errors in my code please tell me what is wrong
import os
data_path = os.getcwd()+'\\data_last\\'
print(data_path)
class Data:
def __init__(self, file):
self.file = file.readlines()
self.dt1, self.dt2, self.dt3 = [], [], []
for i in range(len(self.file)):
self.dt = self.file[i].split(',')
self.dt1 = self.dt1.append(self.dt[2])[1:]
self.dt1 = [float(i) for i in self.dt1]
self.dt2 = self.dt2.append(self.dt[3])[1:]
self.dt2 = [float(i) for i in self.dt2]
self.dt3 = self.dt3.append(self.dt[4])[1:]
self.dt3 = [float(i) for i in self.dt3]
class inputError(Exception):
pass
class Grade(Data):
def avg(self, subject):
if subject == 'dt1':
return sum(self.dt1)/len(self.dt1)
elif subject == 'dt2':
return sum(self.dt2)/len(self.dt2)
elif subject == 'dt3':
return sum(self.dt3)/len(self.dt3)
else:
raise inputError()
def std(self, subject):
if subject == 'dt1':
return (sum([i**2 for i in self.dt1])/len(self.dt1) - self.avg('dt1')**2)**0.5
elif subject == 'dt2':
return (sum([i**2 for i in self.dt2])/len(self.dt2) - self.avg('dt2')**2)**0.5
elif subject == 'dt3':
return (sum([i**2 for i in self.dt3])/len(self.dt3) - self.avg('dt3')**2)**0.5
else:
raise inputError()
def maximum(self, subject):
if subject == 'dt1':
return max(self.dt1)
elif subject == 'dt2':
return max(self.dt2)
elif subject == 'dt3':
return max(self.dt3)
else:
raise inputError
def minimum(self, subject):
if subject == 'dt1':
return min(self.dt1)
elif subject == 'dt2':
return min(self.dt2)
elif subject == 'math':
return min(self.dt3)
else:
raise inputError
weath = dict()
for i in range(2019,2020):
for j in range(5,6):
for k in range(24,27):
name = str(i)+'_'+str(j)+'_'+str(k)
weath[name] = Grade(open(data_path+name+'.csv','r'))
for i in range(2019,2020):
for j in range(5,6):
for k in range(24,27):
name = str(i)+'_'+str(j)+'_'+str(k)
print('{0}year {1}month {2}day temp avg: {3}, std : {4}, max : {5}, min : {6}\n'.format(i, j, k, weath[name].avg('dt1'), weath[name].std('dt1'), weath[name].maximum('dt1'), weath[name].minimum('dt1')))
print('{0}year {1}month {2}day temp avg: {3}, std : {4}, max : {5}, min : {6}\n'.format(i, j, k, weath[name].avg('dt2'), weath[name].std('dt2'), weath[name].maximum('dt2'), weath[name].minimum('dt2')))
print('{0}year {1}month {2}day temp avg: {3}, std : {4}, max : {5}, min : {6}\n'.format(i, j, k, weath[name].avg('dt3'), weath[name].std('dt3'), weath[name].maximum('dt3'), weath[name].minimum('dt3')))
print('\n')
print('\n')