Skip to content Skip to sidebar Skip to footer

How To Make A Loop Over And Over Until We Stop Or Pause It

I recently asked a question here how can I read each line of a xls file with pausing and I got an answer which solved my problem. The code is as follows import time import pandas

Solution 1:

Just put it inside another loop. For example, if you want to repeat the entire thing 10 times, just do this:

for i in xrange(10):    
    for sheet in workbook.sheets():
        num_of_cells = sheet.nrows * sheet.ncols
        for cell in range(num_of_cells):
            row = int(cell / sheet.ncols)
            col = cell % sheet.ncols
            print "value::: ", sheet.cell(row, col).value
            time.sleep(5.5)    # pause 5.5 seconds

Solution 2:

Probably the easiest way I've found is to use python's KeyboardInterrupt exception - that is of course, if you're OK with your whole script halting when you end the loop (by pressing Ctrl-C).

for sheet in workbook.sheets():
    for row inrange(sheet.nrows):
        for column inrange(sheet.ncols):
            try:
                os.system('clear')
                print"value::: ", sheet.cell(row,column).value
                time.sleep(5.5)    # pause 5.5 secondsexcept KeyboardInterrupt:
                pass#insert here anything you'd like your script to do before it halts

Post a Comment for "How To Make A Loop Over And Over Until We Stop Or Pause It"