简体   繁体   中英

try/except while true in a for loop

The following code will gather data from an API and the try/except clause will help to handle several errors (from authentication, index, anything). There's only one error (an authentication error) that I'm using the while True to repeat the API call to make sure I get the data and it will after a try or two. However if by any means I get another error, it'll be infinitely looping and I can't break it so it goes to the next iteration. I tried to create a counter and if the counter reaches to a number then ( pass or continue or break ) but it's not working.

## Create a array to loop to:
data_array_query = pd.date_range(start_date,end_date,freq='6H')

#This is my idea but is not working
#Create a counter
counter = 0

#Loop through the just created array
for idx in range(len(data_array_query)-1):
    ## If counter reaches move on to next for loop element
    while True:
        if counter>=5:
            break
        else:
            try:
                start_date  = data_array_query[idx]
                end_date = data_array_query[idx+1]

                print('from',start_date,'to',end_date)

                df = api.query(domain, site_slug, resolution, data_series_collection, start_date=str(start_date), end_date=str(end_date), env='prod', from_archive=True, phase='production').sort_index()
                print(df.info())
                break
            except Exception as e:
                print(e)
                counter +=1
                print(counter)

在此处输入图像描述

So the output of running this code for a couple of days show that when it runs 5 times (that's the counter max I set up) it does break but it breaks the whole loop and I only want it to move to the next date.

Any help will be appreciated,

You need to use a break statement to get out of a while True loop. pass and continue work for for loops that have a fixed number of iterations. While loops can go on forever (hence the different names)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM