Home > front end >  how to retry from the broken error exception part on a for loop
how to retry from the broken error exception part on a for loop

Time:01-03

This is my latest attempt:

Screenshot from my IDE

stockcode = pd.read_csv('D:\Book1.csv')
stockcode = stockcode.Symbol.to_list()
print(len(stockcode)) 

df = []
for i in stockcode:
    df = isec.get_historical_data(
        interval=time_interval,
        from_date=from_date,
        to_date=to_date,
        stock_code=i,
        exchange_code="NSE",
        product_type="cash"
    )
    df = pd.DataFrame(df["Success"])
    df.append(df)
df = pd.concat(df)

I am trying to fetch data of multiple stocks from brokers API but after few mins it throws an error.

how can it restart from the part where it stopped?

I tried some loop exception but of no use and the error thrown was Output exceeds the size limit Screenshot from my IDE

CodePudding user response:

If I understand you correctly, you want to restart with no looping through items you've already processed? In this case, I'd use manual retry in a code and looping through a copy of your original list without items which are already OK:

number_of_retries = 3
retry_counter = 0
while retry_counter < number_of_retries and not is_success:
    try:
        stockcode_process = stockcode.copy()
        for i in stockcode_process:
            # your for-loop body here with the additional last line as following to remove processed item:
            stockcode.pop(i)
        is_success = true
    except Exception:
        retry_counter  = 1
  • Related