2

I am am looking to restart an iteration if i receive an exception. (... it is reading data from a server, and occasionally gets intermittent error codes, that do not repeat on a retrying).

 with open(input, 'rb') as f:
     r = unicodecsv.reader(f)

 for row in r:

    code to request some data from server

    if response_code == 200:
       code to process response
    else:
       want to restart the iteration for the current row

If i was using a while loop, this sort of thing would obviously be obvious (e.g. don't increment the number), but given I am iterating over row in a for loop, can't think of a way of forcing a re-do of the current iteration.

Although there are lots of similar sounding titled posts (e.g. how to restart "for" loop in python ? , Python - Way to restart a for loop, similar to "continue" for while loops? , python: restarting a loop ) each of the ones I have found/read seems to get at something different (e.g. just how to restart when get to the end, rather than restarting an iteration should a certain condition occur).

[Python 2.7]

Community
  • 1
  • 1
kyrenia
  • 5,431
  • 9
  • 63
  • 93
  • You can't rewind the iteration of a for-loop because a for-loop iterates over an [iterator](https://docs.python.org/3/glossary.html#term-iterator). Iterators can only be advanced to the next position. What's wrong with using a while-loop like you said in your question? –  Jan 15 '15 at 17:18
  • why is a while not an option? – Padraic Cunningham Jan 15 '15 at 17:23
  • @iCodez and Padraic - having seen that there is no way of easily doing this with a for loop (which i had assumed there would be), the simplest answer may well be to switch over to a while (... just a little more complex / not quite as tidy as hoping, but still may be best solution) – kyrenia Jan 15 '15 at 17:27

2 Answers2

9

You can add a second loop like so:

for row in r:
   while True:
       do stuff
       if error:
           continue
       else:
           break
Aran-Fey
  • 39,665
  • 11
  • 104
  • 149
  • I don't understand this logic. Say we are on the first row, we enter the while loop. Next do stuff, there was an error in this case. We continue, which takes us to the next iteration of the while loop. Nothing has changed, we continue to do stuff on the first row, and get errors each time. This does not reset the outer for loop. – Mike Jan 15 '15 at 17:37
  • 2
    It's not supposed to advance the outer loop. OP is contacting a server - if there's a network error, the server has to be contacted again. – Aran-Fey Jan 15 '15 at 17:41
0

This will restart the for-loop at 4 when the iteration reaches 5 and continue with seemingly no break.

def testloop(start, fault_data, maxrange=10, reset_fault_data=0):
    for x in range(start, maxrange):
        if x == fault_data:
            x -= 1
            testloop(x+1, reset_fault_data, maxrange)
            break
        print(x, end=", " if not x == maxrange-1 else " ")
testloop(1, 5, 21)

output:

1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20

without this:

testloop(x+1, reset_fault_data, maxrange)
break

output:

1, 2, 3, 4, 4, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20

structure
  • 35
  • 7