Advertisement
Guest User

Untitled

a guest
Oct 20th, 2019
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.50 KB | None | 0 0
  1. ns and adding input and output files
  2. # ===============================================
  3. import csv as c
  4. import math as m
  5.  
  6. inDataFile = '2019_09_calgary_hourly.csv'
  7. outFileName = 'results.txt'
  8.  
  9. # ===============================================
  10. # declaring variables for column values in the csv
  11. # ===============================================
  12.  
  13. FIELD_YEAR = 5
  14. FIELD_MONTH = 6
  15. FIELD_DAY = 7
  16. FIELD_HOUR = 8
  17. FIELD_TEMP = 9
  18.  
  19. # ===============================================
  20. # requesting user input data for date and converting those values to strings
  21. # ===============================================
  22.  
  23. userDataStr = input('Enter a YEAR, Month and day e.g. 2019-09-01: ').split('-')
  24.  
  25. userYear = int(userDataStr[0])
  26. userMonth = int(userDataStr[1])
  27. userDay = int(userDataStr[2])
  28.  
  29. # ===============================================
  30. # decalaring coldest temp and hour values and converting coldest tempt to a str
  31. # ===============================================
  32.  
  33. currentColdestTemp = float(9999999999.99)
  34. currentColdestHour = ['Not set yet!']
  35.  
  36. # ===============================================
  37. # with loop to open the in datafile and out datafiles with a csv read and skipping the headers of the csv file
  38. # ===============================================
  39.  
  40. with open(inDataFile, 'r') as datafile, open(outFileName, 'w') as resultsFile:
  41. csvFile = c.reader(datafile, dialect='excel')
  42. next(csvFile, None)
  43. # ===============================================
  44. # for loop that includes a nested if statement for comparing user and csv data
  45. # ===============================================
  46. for record in csvFile:
  47. fileYear = record[FIELD_YEAR]
  48. fileMonth = record[FIELD_MONTH]
  49. fileDay = record[FIELD_DAY]
  50. fileHour = record[FIELD_HOUR]
  51. fileTemp = float(record[FIELD_TEMP])
  52. if fileYear == userYear and fileMonth == userMonth and fileDay == userDay:
  53. # ===============================================
  54. # 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
  55. # ===============================================
  56. if fileTemp < currentColdestTemp:
  57. list.clear(currentColdestHour)
  58. currentColdestHour.append(fileHour)
  59. currentColdestHour.append(fileTemp)
  60. currentColdestTemp = fileTemp
  61. resultsFile.write(f'The coldest hour is : {currentColdestHour}\n')
  62. print('Done...')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement