Data Source: RP5 website
Download Data style: select UTF-8 encoding. unzip to get xxx.csv file after download
Calculation target: Contains the average temperature per month and the average temperature of each day of the current month, named by month and stored in the corresponding file.
Data format:
Attention:
- daily average temperature includes times: 02,08,14,20. The initial data contains no use when 05,11,17,23 in the time.
- There may be missing times for some dates in the file, and if so, the day is not calculated. The total number of days to calculate the average monthly temperature does not include this day of absence.
Code:
1 #!/usr/bin/env Python32 defcalctemp (t_file):3With open (T_file,'R') as F:4line =F.readline ()5 whileLine.startswith ('#'):6line =F.readline ()7Time = (' -',' -',' ,',' Geneva')8line =F.readline ()9 whileTrue:Ten if notLine : One Break AL, _sum, month = [], 0, Line[4:6] -result = Open ('averagetemp'+ Month,'W') - whileTrue: theDay = Line[1:3] - forIinchRange (4): - ifLINE[12:14]! =Time[i]: - Break + Try: -_sum + = float (Line.split (';') [1].replace ('"',"')) + except: A Break at ifI! = 3: - F.readline () -line =F.readline () - Else: - Result.writelines ( -Day +':'+ STR (ROUND (_SUM/4, 1)) +'\ n') inL.append (Round (_SUM/4, 1)) -line =F.readline () to_sum =0 + ifLINE[4:6]! =Month: -Result.writelines (month +'average monthly temperature:'+ theSTR (Round (SUM (L)/Len (L), 1))) * result.close () $ BreakPanax Notoginseng Print('completed') - the + if __name__=='__main__': A ImportSYS the ifLen (SYS.ARGV)! = 2: + RaiseValueError ('requires a filename parameter') -Calctemp (Sys.argv[1])
How to use:
Python3 file name. py data file. csv
Calculation Result:
Single File:
Python calculates the daily and monthly average temperature of an international Exchange station