Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ns and adding input and output files
- # ===============================================
- import csv as c
- import math as m
- inDataFile = '2019_09_calgary_hourly.csv'
- outFileName = 'results.txt'
- # ===============================================
- # declaring variables for column values in the csv
- # ===============================================
- FIELD_YEAR = 5
- FIELD_MONTH = 6
- FIELD_DAY = 7
- FIELD_HOUR = 8
- FIELD_TEMP = 9
- # ===============================================
- # requesting user input data for date and converting those values to strings
- # ===============================================
- userDataStr = input('Enter a YEAR, Month and day e.g. 2019-09-01: ').split('-')
- userYear = int(userDataStr[0])
- userMonth = int(userDataStr[1])
- userDay = int(userDataStr[2])
- # ===============================================
- # decalaring coldest temp and hour values and converting coldest tempt to a str
- # ===============================================
- currentColdestTemp = float(9999999999.99)
- currentColdestHour = ['Not set yet!']
- # ===============================================
- # with loop to open the in datafile and out datafiles with a csv read and skipping the headers of the csv file
- # ===============================================
- with open(inDataFile, 'r') as datafile, open(outFileName, 'w') as resultsFile:
- csvFile = c.reader(datafile, dialect='excel')
- next(csvFile, None)
- # ===============================================
- # for loop that includes a nested if statement for comparing user and csv data
- # ===============================================
- for record in csvFile:
- fileYear = record[FIELD_YEAR]
- fileMonth = record[FIELD_MONTH]
- fileDay = record[FIELD_DAY]
- fileHour = record[FIELD_HOUR]
- fileTemp = float(record[FIELD_TEMP])
- if fileYear == userYear and fileMonth == userMonth and fileDay == userDay:
- # ===============================================
- # The following if statement compares the file temp with current coldest temp if file temp is lower the old coldest tempt is cleared and the file is appended
- # ===============================================
- if fileTemp < currentColdestTemp:
- list.clear(currentColdestHour)
- currentColdestHour.append(fileHour)
- currentColdestHour.append(fileTemp)
- currentColdestTemp = fileTemp
- resultsFile.write(f'The coldest hour is : {currentColdestHour}\n')
- print('Done...')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement