vendredi 11 décembre 2020

Trying to filter an api request with <= and >= date tomorrow 0100-2400 if statement

I am new to Python and am having a hard time filtering an api request by datetime: tomorrow basically from hours 0100-2400, only. Time zone is local time. The request prints out as a nested dictionary and I have been able to filter it down to 'validTime' but can't get it filtered anymore from there. My start_time and end_time may be wrong, or my if statement may be wrong, or it's something else. I feel that there is an easy fix for this, I just can't figure it out. Please help. My code is below:

import constants
import requests
import datetime
import json

today = datetime.date.today()
yesterday = today - datetime.timedelta(days=1)
tomorrow = today + datetime.timedelta(days=1)
day_after_tomorrow = today + datetime.timedelta(days=2)
hour1 = datetime.time(1, 0, 0)
hour0 = datetime.time(0, 0, 0)

start_time = ['{}T{}+00:00/PT1H'.format(tomorrow, hour1)]
end_time = ['{}T{}+00:00/PT1H'.format(day_after_tomorrow, hour0)]

response = requests.get(constants.austinfgd, headers=headers)
data = json.loads(response.text)
temperature_data = data['properties']['temperature']['values']

print(temperature_data)

This is what is printed out to this point: Printed response to temperature_data from website

[{'validTime': '2020-12-11T10:00:00+00:00/PT2H', 'value': 17.77777777777778},
{'validTime': '2020-12-11T12:00:00+00:00/PT1H', 'value': 16.666666666666668},
{'validTime': '2020-12-11T13:00:00+00:00/PT2H', 'value': 17.22222222222222}, 
... 

and so on for a number of days

So, now I need to filter by validTime and I specifically want the hours = to and in between start_time and end_time. So, here is my code for that:

for v in temperature_data:
    if ['validTime'] >= start_time and ['validTime'] <= end_time:

At this point, pycharm is telling me to simplifiy the chained comparison and it automatically gives me this:

for v in temperature_data:
    if start_time <= ['validTime'] <= end_time:
        print(v)

At this point, the only thing that prints out is: Printed response to v

Process finished with exit code 0

I need to print these values by these times only, so I can dump into a table in postgresql, to be used elsewhere.

What am I doing wrong? I have been googling this for over a week, and I give up. Time to ask for help, please help. Thank you in advance for your help.

Aucun commentaire:

Enregistrer un commentaire