Home > Mobile >  Change range in string during loop
Change range in string during loop

Time:01-13

I need to query 5000 rows of data, but cannot query the whole 5000. How can I incorporate a loop to change the ranges of this url? The url needs to be modified during each loop to look like 1:1000, 1001:2000, 2001:3000, 3001:4000, 4001:5000.

How can I use a loop and format the string for these ranges?

url = 'https://data.epa.gov/efservice/PUB_DIM_FACILITY/ROWS/1:1000/JSON'
test = pd.read_json(url)

CodePudding user response:

You can use a for loop to modify the range, and then use an f-string for the url:

for i in range(5):
    start = 1 1000*i
    end = 1000*(i 1)
    url = f"https://data.epa.gov/efservice/PUB_DIM_FACILITY/ROWS/{start}:{end}/JSON"
    test = pd.read_json(url)
  •  Tags:  
  • Related