Python calculates the daily and monthly average temperature of an international Exchange station

Source: Internet
Author: User

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:

    1. daily average temperature includes times: 02,08,14,20. The initial data contains no use when 05,11,17,23 in the time.
    2. 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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.