mercredi 29 avril 2020

Python: Why is "else" statement not accepting a string?

I am trying to calculate average daily windspeed from a file with hourly windspeed. To do this I am using for loops to identify month&day combinations and then analyze the hourly wind data from each calendar day. The hourly data either has a positive value, or -999 if the data is missing. I wrote the following line of code, which successfully filters out any missing data during a 24 hour period and returns the average for the day:

Average_WindSpeed = sum(row[1] == m and row [2] == d and float(row[4]) if float(row[4]) > -999 else 0 for row in reader)/24

My issue is that the else condition of the code will only let me enter a number. If I enter something like else: 'Missing' I get a TypeError: unsupported operand type(s) for +: 'int' and 'str' message. I know that the TypeError means that there is some combination of numbers and sting that is incompatible, I just don't understand how its occurring in an else statement. I am trying to set up the if statement so that if all 24 hours in the day are -999, else will return "Missing". The code works with the else: 0 place holder that I have put in, however, the numerical value that I am having to pass is erroneous. This is the pertinent section of code within a larger project:

import csv

print("Month", "Day", "WindSpeed")
months = ['01']

for m in months:
    if m == '01' or m =='03' or m == '05' or m == '07' or m == '08' or m == '10' or m == '12':
         dates = ['01','02','03','04','05','06','07','08','09','10','11','12','13','14','15','16','17','18','19','20','21','22','23','24','25','26','27','28','29','30','31']
         for d in dates:

             # Calculate average daily windspeed from hourly data
             with open('samplehourlydata.txt', 'r') as f:
                reader = csv.reader(f)
                next(reader)
                Average_WindSpeed = sum(row[1] == m and row [2] == d and float(row[4]) if float(row[4]) > -999 else 0 for row in reader)/2
                print(Average_WindSpeed)

If it helps, here's some sample data:

yyyy,mm,dd,hour,W Speed
1941,01,01,00,-999
1941,01,01,01,-999
1941,01,01,02,-999
1941,01,01,03,-999
1941,01,01,04,-999
1941,01,01,05,-999
1941,01,01,06,-999
1941,01,01,07,-999
1941,01,01,08,-999
1941,01,01,09,-999
1941,01,01,10,-999
1941,01,01,11,-999
1941,01,01,12,-999
1941,01,01,13,-999
1941,01,01,14,-999
1941,01,01,15,-999
1941,01,01,16,-999
1941,01,01,17,-999
1941,01,01,18,-999
1941,01,01,19,-999
1941,01,01,20,-999
1941,01,01,21,-999
1941,01,01,22,-999
1941,01,01,23,-999
1941,01,02,00, 9.2
1941,01,02,01,11.5
1941,01,02,02, 9.2
1941,01,02,03,11.5
1941,01,02,04,11.5
1941,01,02,05, 8.1
1941,01,02,06, 9.2
1941,01,02,07,11.5
1941,01,02,08, 9.2
1941,01,02,09,10.4
1941,01,02,10,12.7
1941,01,02,11,13.8
1941,01,02,12,11.5
1941,01,02,13,10.4
1941,01,02,14,11.5
1941,01,02,15,11.5
1941,01,02,16,10.4
1941,01,02,17, 6.9
1941,01,02,18, 5.8
1941,01,02,19, 9.2
1941,01,02,20,10.4
1941,01,02,21,11.5
1941,01,02,22, 4.6
1941,01,02,23, 9.2

Also, I know that there are other ways to do this such as possibly setting up a dictionary, or using numpy, however I'm really trying to understand this "else" situation. Thanks.

Aucun commentaire:

Enregistrer un commentaire