0

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')
haejin
  • 9
  • 2
  • 1
    ``append`` mutate the list and returns ``None``. Assigning the result of ``append`` or slicing it with ``[1:]`` does not make sense. – MisterMiyagi Jun 07 '19 at 11:42

1 Answers1

-1

The error is because you are trying to use subscripting on a method:

self.dt1 = self.dt1.append(self.dt[2])[1:]

.append()

is a method for lists and you can't use slicing with methods unless they are returning something.

I still don't understand why you need to use slicing after appending but if you provide the details maybe I could give a workaround.

P.S. : Unless this is the only way it needs to be done, I would recommend using Pandas library for the task, you can efficiently extract the data from an excel file as well as find the statistical measures with few lines of code.

  • place date temp wind humid 112 2019-05-24 1:00 17.5 1.1 65 112 2019-05-24 2:00 17.5 2.2 66 the form of excel is like this. so, i want remove the first line. That's why i did slicing! – haejin Jun 07 '19 at 12:30