[Tutor] A Python program to Summarise weather results?
Peter Otten
__peter__ at web.de
Thu Aug 30 05:02:07 EDT 2018
Matthew Polack wrote:
> Hi,
>
> We have a spreadsheet from a local weather station.....it summarises the
> amount of rainfall per day since 1863.
>
>
http://www.bom.gov.au/jsp/ncc/cdio/weatherData/av?p_nccObsCode=136&p_display_type=dailyDataFile&p_startYear=1863&p_c=-1249186659&p_stn_num=079028
>
> We'd love to be able to summarise this into years eg. Total rainfall of
> 1863, 1864 all the way through to 2018.
>
> Would Python be able to do this using the csv file?
Yes.
Python has powerful tools to make this concise, like csv.DictReader() and
itertools.groupby(), or -- even more highlevel -- pandas.
But you want to learn Python, don't you?
Therefore I recommend that you try to solve this with just open() and a for
loop to read the lines, and the str.split() method to break the lines into
fields. With 'if ....' you check whether the year has changed so that you
need to print the current total and reset the total rainfall.
Give it a try, see how far you get, and we'll help you over the obstacles
once you can present some rudimentary code.
More information about the Tutor
mailing list