在“ data_last”文件夹中,有三个excel文件,分别显示位置,日期,温度,风速,湿度。 使用这些数据,我想通过使用类来制作每个平均,标准,最大,最小prgram。 但是错误。。。
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
我认为我的代码中有很多错误 请告诉我哪里错了
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')
答案 0 :(得分:-1)
错误是因为您尝试在方法上使用下标:
self.dt1 = self.dt1.append(self.dt [2])[1:]
.append()
是用于列表的方法,除非方法返回了值,否则不能对它使用切片。
我仍然不明白为什么附加后需要使用切片,但是如果您提供详细信息,也许我可以提供解决方法。
P.S。注意:除非这是唯一需要完成的方法,否则我建议使用Pandas库执行此任务,您可以从excel文件中高效提取数据,并且只需几行代码即可找到统计量。