0

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(',') 
6
  • 1
    Why not if not rain:? And have you checked that some data should be written out? Commented Jun 7, 2014 at 15:50
  • I would expect "rain == 0" to mean "no rain." Have you tried "rain != 0"? Commented Jun 7, 2014 at 15:51
  • print the value of rain to determine what you should check for -- perhaps it's an empty string or something. Commented Jun 7, 2014 at 16:04
  • Try "print type(rain), rain" right before the if. You'll likely find that rain is a string, not an int, so the compare doesn't work. Commented Jun 7, 2014 at 16:17
  • Yes, I tried rain == “0” ... That didn’t solve it. rain == 0 does mean no rain, which is what I want. Commented Jun 7, 2014 at 19:18

2 Answers 2

1

It is odd to use an index (j) like that, and why parse the data if you don't want to write it out - why not:

for obs in parsed_json['history']['observations']:
    if obs['rain']: # or 'if not', depending on which you want
        hours = obs['date']['hour']
        ...
        myfile.write(...)
        ...

Without seeing a sample of the data, it is hard to know whether or not anything should end up in myfile.

Sign up to request clarification or add additional context in comments.

Comments

0

It's not a syntax issue like using === vs ==, is it? Try using 'snow' or another variable and/or use a debug statement to show the value of rain. This assumes the output statements to the CSV file work. It's been a while since I last looked at Python. You can force rain to be 0 then check if the records get added to the CSV to make sure the output works. Then work back to determine when/why rain is not 0. Hope this is useful and sparks other ideas.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.