I am using the Weather Underground API to write historical weather data to a csv. I am not having any issues with my Python Program. However, I want to add a conditional statement that tells my program to only write weather observations that include rain. So, if it’s raining in Detroit on observation j, then write the hour, conditions, rain, snow, temp_f.
Below is the last section of the program. “for j in range” loops through the weather observations on a given day. “rain” is binary.
Again, everything else in the program works great. The only change that I’ve made to a working program, is the “if rain == 0:”...and now it doesn’t write the data to my csv. There are no error messages.
Any help is greatly appreciated.
Paul
for j in range(len(parsed_json['history']['observations'])):
hour = parsed_json['history']['observations'][j]['date']['hour']
conditions = parsed_json['history']['observations'][j]['conds']
rain = parsed_json['history']['observations'][j]['rain']
snow = parsed_json['history']['observations'][j]['snow']
temp_f = parsed_json['history']['observations'][j]['tempi']
if rain == 0:
myfile.write('\n')
myfile.write(str(dd[i]))
myfile.write(',')
myfile.write(str(hour))
myfile.write(',')
myfile.write(str(cities[b]))
myfile.write(',')
myfile.write(str(temp_f))
myfile.write(',')
myfile.write(str(conditions))
myfile.write(',')
myfile.write(str(rain))
myfile.write(',')
myfile.write(str(snow))
myfile.write(',')
if not rain:? And have you checked that some data should be written out?printthe value ofrainto determine what you should check for -- perhaps it's an empty string or something.