Hello I need some python programming help. Please post a snapshot of python scre
ID: 675404 • Letter: H
Question
Hello I need some python programming help. Please post a snapshot of python screen. Thank you!
I have csv file that is named: "precipitation.csv". The data within the file is below (It will be great if you can copy and paste the following data in a csv file):
20091005,5.8
20091007,0.0
20091008,NA
20091123,5.3
20091130,5.4
20091121,NA
20101005,7.9
20101009,7.7
20101013,8.3
20101005,5.8
20101007,0.0
20101030,NA
Each line is formatted like this: YYYYMMDD, daily precipitation. The requirement to sum the monthly precipitation for each year and then divide by 2 to get the average monthly precipitation. For example, add all the daily precipitations of 2009 October and then add all the precipitation of 2010 October. Then add these two sums and divide by two(since there are two years 2009 and 2010). Attention: NA does not take into the calculation.
Explanation / Answer
def getDataList(fileName):
dataFile = open(fileName, "r")
dataList = []
for line in dataFile:
dataList.append(line.strip().split(','))
return dataList
def monthavg(datalist):
d= datalist
monthlyaveragelist= []
tv=0
vta=0
month= d[0][0][5:7]
year= d[0][0][0:4]
for i in d:
v= float(i[5])
c= float(i[6])
if i[0][5:7] == month and i[0][0:4] == year:
vtimesc= v*c
tv= tv + v
vta= vta + vtimesc
ap= vta/tv
aptuple= (ap,i[0][0:7])
monthlyaveragelist.append(aptuple)
return monthlyaveragelist
b = getDataList('precipitation.csv')
b.pop(0)
c=monthavg(b)
print c
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.