0

I keep running into this error when I try to run my code.

Script:

start_date = int(input("Enter Start Date (YYYYDDMM)"))
end_date = int(input("Enter End Date (YYYYDDMM)"))
date_range = range(start_date, end_date + 1,1)

length = len(date_range)
i=0

for x in date_range:
   hist_data = requests.get("https://covidtracking.com/api/v1/us/" + str(x[i]) + ".json")
   for data in hist_data:
       print(f"x - {data['state']} - Deaths: {data['death']} - Increase in Deaths: {data['deathIncrease']}")
       i+=1

this is my traceback:

Traceback (most recent call last):
  File "C:/Users/****/PycharmProjects/COVID_API/API_REQUESTS.py", line 18, in <module>
    hist_data = requests.get("https://covidtracking.com/api/v1/us/" + str(x[i]) + ".json")
TypeError: 'int' object is not subscriptable

I was wondering how I would intake the dates as an int, populate the list for the dates in between, then feed the list through the for loop so I can get the data for each respective date. thank you!

thouxand
  • 5
  • 2

3 Answers3

0

for x in date_range returns already one entry per iteration. You do not have to index x with i. x is already one entry of the range.

Try this one

for x in date_range:
   hist_data = requests.get("https://covidtracking.com/api/v1/us/" + str(x) + ".json")
   for data in hist_data:
       print(f"x - {data['state']} - Deaths: {data['death']} - Increase in Deaths: {data['deathIncrease']}")

So you don't need i and length

You could have written:

date_range = list(range(start_date, end_date + 1,1))

for i in range(len(date_range):
    x = date_range[i]
    hist_data = requests.get("https://covidtracking.com/api/v1/us/" + str(x[i]) + ".json")

But this is less elegant then your above code with the i and the indexing removed

Please note, that this will not work nicely if you want to loop for example from 20200430 to 20200510.

You would get dates like 20200431, 20200432, 20200433, .. 20200499

Better to use the datetime module and do something like:

from datetime import datetime
from datetime import timedelta

start_date = int(input("Enter Start Date (YYYYDDMM)"))
end_date = int(input("Enter End Date (YYYYDDMM)"))
print("as_string:", start_date, end_date)
start_date = datetime.strptime(start_date, "%Y%m%d").date()
end_date = datetime.strptime(end_date, "%Y%m%d").date()
print("as date objects:", start_date, end_date)

date = start_date
while date <= end_date:
    date_str = date.strftime("%Y%m%d")
    hist_data = requests.get("https://covidtracking.com/api/v1/us/" + date_str + ".json")
    print(date_str)
    date += timedelta(days=1)

you can also look at Iterating through a range of dates in Python for using a for loop instead of a while loop that will let you loop through a range of dates.

gelonida
  • 5,327
  • 2
  • 23
  • 41
  • Hello, Thank you so much for your suggestion! I have implemented it, however I run into an error starting at the strptime function. ```Traceback (most recent call last): File "C:/Users/*****/PycharmProjects/COVID_API/API_REQUESTS.py", line 15, in start_date = datetime.strptime(start_date, "%y%m%d").date() File "C:\Users\*****\anaconda3\envs\COVID_API\lib\_strptime.py", line 568, in _strptime_datetime tt, fraction, gmtoff_fraction = _strptime(data_string, format) ValueError: time data '20200408' does not match format '%y%m%d' ``` – thouxand May 15 '20 at 17:13
  • The `y` must be an uppercase `Y`. So you should use `"%Y%m%d"` instead of `"%y%m%d"` Please look at https://docs.python.org/3.8/library/datetime.html#strftime-and-strptime-format-codes to have the detailed documentation about the format strings. `"%Y%m%d"` can parse strings like `"20200408"` whereas `"%y%m%d"` can parse strings like `"200408"` – gelonida May 15 '20 at 22:46
0

You are trying to iterate over range(start_date, end_date + 1,1). Hence, the type of x variable in for loop is 'int' and 'int' type are not subscriptable which means they cannot be indexed.

To iterate over date you could use timedelta.

import datetime
from datetime import timedelta

start_date = datetime.date(2020, 2, 5) #Start Date
n=10 #Interval
for each_date in (start_date + timedelta(n) for n in range(day_count)):
    print(each_date)

Output:

2020-02-05
2020-02-06
2020-02-07
2020-02-08
2020-02-09
2020-02-10
2020-02-11
2020-02-12
2020-02-13
Damnik Jain
  • 374
  • 2
  • 11
0

this is probably unrelated to your problem, but just to let you know about indexing in looping. you can do it like this

date_range = ['2020-05-01', '2020-05-02', '2020-05-03', '2020-05-04']
for idx, value in enumerate(date_range):
  print(f"This is the index: {idx}, and this is the value: {value}")

output will be:

This is the index: 0, and this is the value: 2020-05-01
This is the index: 1, and this is the value: 2020-05-02
This is the index: 2, and this is the value: 2020-05-03
This is the index: 3, and this is the value: 2020-05-04