# define object for holding a set of temperature data
class TempData():
def __init__(self):
self.day = [] # list of days
self.temp = [] # list of temperature
Now, we create two object instance variables of this type where one will hold temperatures data as in the csv file and the other one will hold temperature differences data.
csv_imported_data = TempData() # create object for holding csv file temperature data
processed_data = TempData() # create object for holding processed data
Now, we can open the csv file and load the csv columns data into the object lists for days and temperatures data.
csv_file = open('data.csv', 'r') # open the csv file for reading
reader = csv.reader(csv_file) # pass the file to csv reader object for extracting data
for row in reader: # get the row data from the csv file
csv_imported_data.day.append(float(row[0])) # get the first column data
csv_imported_data.temp.append(float(row[1])) # get the second column data
csv_file.close() # close the csv file
Now, lets show the temperature data from the object:
# print temperature data from the temperature object
for i in range(len(csv_imported_data.day)):
print(csv_imported_data.day[i], csv_imported_data.temp[i])
Now, lets show the average temperature from the object's temperature data:
# show average temperature from the temperature object
print('Average temperature for', len(csv_imported_data.day), 'days was', sum(csv_imported_data.temp)/len(csv_imported_data.temp))
Now, lets find the temperature difference from the object's temperature data and store difference data into another object of same type:
# find difference between temperatures and store in temperature data object
for i in range(len(csv_imported_data.day)):
if (i == 0): # no difference for first data
processed_data.day.append(csv_imported_data.day[i])
processed_data.temp.append(0)
else:
processed_data.day.append(abs(csv_imported_data.day[i] - csv_imported_data.day[i-1]))
processed_data.temp.append(abs(csv_imported_data.temp[i] - csv_imported_data.temp[i-1]))
Now, we show the temperature difference data from the object:
# print temperature difference data from the temperature object
for i in range(len(processed_data.day)):
print(processed_data.day[i], processed_data.temp[i])
Finally, we save the temperature difference data from the object to a new csv file so that we can retrieve the temperature difference data without running the Python script again:
csv_file = open('processed_temp_data.csv', 'w', newline='') # create a new csv file for writing
csv_writer = csv.writer(csv_file) # pass the file to csv writer object for writing data
for row in zip(processed_data.day, processed_data.temp): # create the row data from the the temperature object
csv_writer.writerow(row)
csv_file.close()