I currently have an excel sheet checking another excel sheet for certain numbers. Once it finds the matching cell, I want it to move on back to the very first for loop. if it doesn't find the matching cell, but finds the first 6 digits of it, I want it to mark it as checked and then move back to the first for loop again.
How is this done?
Below is my code. I commented where I wanted it to go back to the first for
loop I made.
for row in range(sheet.nrows):
cell = str(sheet.cell_value(row, 0))
if fd.match(cell):
for rows in range(sheet.nrows):
windcell = str(windc_sheet.cell_value(rows, 0))
if fd.match(windcell):
if cell == windcell:
outputsheet.write(row, 1, ' ')
#GO BACK TO FIRST FOR LOOP
else:
sixdig = cell[0:6]
sixdigwind = windcell[0:6]
if sixdig == sixdigwind:
outputsheet.write(row, 1, 'Check')
#GO BACK TO FIRST FOR LOOP
else:
sixdig = cell[0:6]
for rows in range(sheet.nrows):
windcell = str(windc_sheet.cell_value(rows,0))
sixdigwind = windcell[0:6]
if sixdig == sixdigwind:
outputsheet.write(row, 1, 'Check')
Just use the break
statement. It breaks you out of a for
loop or while
loop.
From the Python docs:
break may only occur syntactically nested in a for or while loop, but not nested in a function or class definition within that loop. It terminates the nearest enclosing loop , skipping the optional else clause if the loop has one. If a for loop is terminated by break, the loop control target keeps its current value. When break passes control out of a try statement with a finally clause, that finally clause is executed before really leaving the loop.
(Emphasis mine)
for row in range(sheet.nrows):
cell = str(sheet.cell_value(row, 0))
if fd.match(cell):
for rows in range(sheet.nrows):
windcell = str(windc_sheet.cell_value(rows, 0))
if fd.match(windcell):
if cell == windcell:
outputsheet.write(row, 1, ' ')
break # TERMINATE ENCLOSING FOR LOOP
else:
sixdig = cell[0:6]
sixdigwind = windcell[0:6]
if sixdig == sixdigwind:
outputsheet.write(row, 1, 'Check')
break # TERMINATE ENCLOSING FOR LOOP
else:
sixdig = cell[0:6]
for rows in range(sheet.nrows):
windcell = str(windc_sheet.cell_value(rows,0))
sixdigwind = windcell[0:6]
if sixdig == sixdigwind:
outputsheet.write(row, 1, 'Check')
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.