What I'm trying to do is to take the data that is read from filteredData.csv and and run through and figure out how to find the average amount of snow for each location within the year of 2016. Then I want to take that data and load it into a new csv file called average2016.csv.
I have currently tried to open up theaverage2016.csv within the open file of filteredData and tried to loop in the location and average snow.
data2 = open('average2016.csv','w')
for row in csv1:
print (location + "," + average_snow)
data2.close()
My whole code looks like this:
import csv
data = open('filteredData.csv','r')
# Create Dictionaries to store location values
# Snow_Fall is the number of inches for that location
# Number_Days is the number of days that there is Snowfall data for
Snow_Fall = {}
Number_Days = {}
# Create CSV reader
csv1 = csv.DictReader(data,delimiter=',')
# read each row of the CSV file and process it
for row in csv1:
# Check the date column and see if it is in 2016
if "2016" in row["DATE"]:
# Check to see if the value in the snow column is null/none if so then skip processing that row
if (row["SNOW"] is None) or (row["SNOW"] == ""):
pass
else:
# Check to see if the location has been added to the dict if it has then add the data to itself
# If it has not then just assign the data to the location.
if row["NAME"] in Snow_Fall:
Snow_Fall[row["NAME"]] = Snow_Fall[row["NAME"]] + float(row["SNOW"])
Number_Days[row["NAME"]] = Number_Days[row["NAME"]] + 1
else:
Snow_Fall[row["NAME"]] = float(row["SNOW"])
Number_Days[row["NAME"]] = 1
# For each location we want to print the data for that location
for location in Snow_Fall:
print ("The number of inches for location " + location + " is " + str(Snow_Fall[location]))
print ("The number of days of snowfall for location " + location + " is " + str(Number_Days[location]))
print ("The average Number of Inches for location " + location + " is " + str(Snow_Fall[location] / Number_Days[location]))
data2 = open('average2016.csv','w')
for row in csv1:
print (location + "," + average_snow)
data2.close()
data.close()

print('stuff', file=data2)docs