#!/usr/bin/env python3 # -*- coding: utf-8 -*- """ Felix Muzny 10/21/2022 DS 2000 Lecture 13 - functions and program design, HW 5 help A program to read and analyze some small weather data """ WEATHER_DATA = "weather_sample.csv" def read_data(filename, skip_header = False): """ Reads in the data in a given file and stores the values in a list of lists of strings. Assumes that commas separate row items in the given file. Parameters ---------- filename : string name of the file skip_header: boolean, optional whether or not to skip a header row. Default to False. Returns ------- data : list of lists list of lists of string values for all lines in the file """ file = open(filename, "r") data = [] # do we need to skip the first row? if skip_header: file.readline() for line in file: pieces = line.strip().split(",") # replace empty strings with 0s (for all columns) # we know empty strings happen in this column # use a for loop if you want to do this for all columns! if pieces[2] == "": pieces[2] = "0.0" # change temp to ints pieces[1] = int(pieces[1]) # change precip to floats pieces[2] = float(pieces[2]) # change temp to celcius # C = 5/9(F-32) pieces[1] = (5/9) * (pieces[1] - 32) # make sure I only have the data that I want? # build a new list for this row row = [pieces[0], pieces[1], pieces[2]] data.append(row) return data def main(): print("fake HW 5/Weather data analysis") weather_data = read_data(WEATHER_DATA, skip_header=True) print(weather_data[:4]) main()