Advertisement
silver2row

Weather_and_Split.py

Sep 8th, 2024
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.75 KB | None | 0 0
  1. #!/usr/bin/python3
  2.  
  3. # Reading from Weather.txt and making adjustments
  4.  
  5. #### Read in Data ####
  6. filename = input("Please enter in your file name for data conversion: ")
  7. infile = open(filename, 'r')
  8.  
  9. #### Read Line from File ####
  10. datalist = []
  11.  
  12. for line in infile:
  13.     #### Get Data from Line ####
  14.     date, h, l, r = (line.split(','))
  15.     highTemp = int(h)
  16.     lowTemp  = int(l)
  17.     rainfall = float(r)
  18.     #### Get Date Correct ####
  19.     month, year = (date.split('-'))
  20.     month = int(m)
  21.     year  = int(y)
  22.  
  23.     #### Put data in a List ####
  24.     datalist.append([month, year, highTemp, lowTemp, rainfall])
  25.  
  26. # Close the File #
  27. infile.close()
  28.  
  29. #### Analyze the Data ####
  30. month = int(input("For the median of high-low temps and median rainfall, enter in the month of your choosing: \n"))
  31. year  = int(input("Please enter the year of 2024! "))
  32.  
  33. # Search for Historical Data #
  34. gooddata = []
  35. for singleday in datalist:
  36.     if (singleday[0] == month) and (singleday[1] == year):
  37.         gooddata.append([singleday[2], singleday[3], singleday[4], singleday[5]])
  38.  
  39. # Perform Analysis #
  40. minsofar     = 120
  41. maxsofar     = -100
  42. numgooddates = 0
  43. sumofmin     = 0
  44. sumofmax     = 0
  45. for singlemonth in gooddata:
  46.     numgooddates += 1
  47.     sumofmin += singleday[1]
  48.     sumofmax += singleday[2]
  49.     if singlemonth[1] < minsofar:
  50.         minsofar = singleday[1]
  51.     if singlemonth[2] > maxsofar:
  52.         maxsofar = singleday[2]
  53.  
  54. avgLow  = sumofmin / numgooddates
  55. avgHigh = sumofmax / numgooddates
  56.  
  57. #### Present the Results ####
  58. print("There were", numgooddates, "days")
  59. print("The lowest temperature on record was", minsofar)
  60. print("The highest temperature on record was", maxsofar)
  61. print("The average high was", avgHigh)
  62. print("The average low was", avgLow)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement