Breaking out of one for loop and continuing the other code
<p>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. </p>

<p>How is this done? </p>

<p>Below is my code. I commented where I wanted it to go back to the first <code>for</code> loop I made.</p>

<pre><code>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')
</code></pre>

<p>Just use the <code>break</code> statement. It breaks you out of a <code>for</code> loop or <code>while</code> loop. </p>

<p>From the Python docs:</p>

<blockquote>
  <p>break may only occur syntactically nested in a for or while loop, but not
  nested in a function or class definition within that loop. <strong>It terminates the
  nearest enclosing loop</strong>, 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.</p>
</blockquote>

<p>(Emphasis mine)</p>

<pre><code>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')
</code></pre>

python for-loop break