How to skip one part of a single loop iteration in Python

python skip loop iteration
python break out of function
python while loop break
python skip line in for loop
how to end a while loop in python
continue outside of loop python
how to end a for loop in python
python exit if statement

I am creating about 200 variables within a single iteration of a python loop (extracting fields from excel documents and pushing them to a SQL database) and I am trying to figure something out.

Let's say that a single iteration is a single Excel workbook that I am looping through in a directory. I am extracting around 200 fields from each workbook.

If one of these fields I extract (lets say field #56 out of 200) and it isn't in proper format (lets say the date was filled out wrong ie. 9/31/2015 which isnt a real date) and it errors out with the operation I am performing.

I want the loop to skip that variable and proceed to creating variable #57. I don't want the loop to completely go to the next iteration or workbook, I just want it to ignore that error on that variable and continue with the rest of the variables for that single loop iteration.

How would I go about doing something like this?

In this sample code I would like to continue extracting "PolicyState" even if ExpirationDate has an error.

Some sample code:

import datetime as dt
import os as os
import xlrd as rd

files = os.listdir(path)

for file in files: #Loop through all files in path directory  
            filename = os.fsdecode(file) 
            if filename.startswith('~'): 
                continue

            elif filename.endswith( ('.xlsx', '.xlsm') ): 
                try:
                    book = rd.open_workbook(os.path.join(path,file)) 
                except KeyError:
                    print ("Error opening file for "+ file) 
                    continue

                    SoldModelInfo=book.sheet_by_name("SoldModelInfo")
                    AccountName=str(SoldModelInfo.cell(1,5).value)
                    ExpirationDate=dt.datetime.strftime(xldate_to_datetime(SoldModelInfo.cell(1,7).value),'%Y-%m-%d')
                    PolicyState=str(SoldModelInfo.cell(1,6).value)
                    print("Insert data of " + file +" was successful")
            else:
               continue               

As suggested you could use multiple try blocks on each of your extract variable, or you could streamline it with your own custom function that handles the try for you:

from functools import reduce, partial

def try_funcs(cell, default, funcs):
    try:
        return reduce(lambda val, func: func(val), funcs, cell)
    except Exception as e:
        # do something with your Exception if necessary, like logging.
        return default

# Usage:

AccountName = try_funcs(SoldModelInfo.cell(1,5).value, "some default str value", str)
ExpirationDate = try_funcs(SoldModelInfo.cell(1,7).value), "some default date", [xldate_to_datetime, partial(dt.datetime.strftime, '%Y-%m-%d')])
PolicyState = try_funcs(SoldModelInfo.cell(1,6).value, "some default str value", str)

Here we use reduce to repeat multiple functions, and pass partial as a frozen function with arguments.

This can help your code look tidy without cluttering up with lots of try blocks. But the better, more explicit way is just handle the fields you anticipate might error out individually.

How to skip one part of a single loop iteration in Python, Using for loops and while loops in Python allow you to automate and repeat that will execute with each iteration of the for loop until the loop breaks, to skip over the part of a loop where an external condition is triggered, but to exaplined quite simple, that's nice; but you have a major error in your code:� The continue statement in Python returns the control to the beginning of the while loop. The continue statement rejects all the remaining statements in the current iteration of the loop and moves the control back to the top of the loop. The continue statement can be used in both while and for loops. Example:

Use multiple try blocks. Wrap each decode operation that might go wrong in its own try block to catch the exception, do something, and carry on with the next one.

            try:
                book = rd.open_workbook(os.path.join(path,file)) 
            except KeyError:
                print ("Error opening file for "+ file) 
                continue

            errors = []

            SoldModelInfo=book.sheet_by_name("SoldModelInfo")
            AccountName=str(SoldModelInfo.cell(1,5).value)
            try:
                ExpirationDate=dt.datetime.strftime(xldate_to_datetime(SoldModelInfo.cell(1,7).value),'%Y-%m-%d')
            except WhateverError as e:
                # do something, maybe set a default date?
                ExpirationDate = default_date
                # and/or record that it went wrong?
                errors.append( [ "ExpirationDate", e ])
            PolicyState=str(SoldModelInfo.cell(1,6).value)
            ...
            # at the end
            if not errors:
                print("Insert data of " + file +" was successful")
            else:
                # things went wrong somewhere above. 
                # the contents of errors will let you work out what

Break, Continue, and Pass Statements in For and While Loops , Python break, continue and pass Statements - You might face a situation in which you need to exit a loop completely when an external condition is triggered or there may a situation when you want to skip a part of the loop and start next execution. The break statement in Python terminates the current loop and resumes� This loop is interpreted as follows: Initialize i to 1.; Continue looping as long as i <= 10.; Increment i by 1 after each loop iteration.; Three-expression for loops are popular because the expressions specified for the three parts can be nearly anything, so this has quite a bit more flexibility than the simpler numeric range form shown above.

So, basically you need to wrap your xldate_to_datetime() call into try ... except

import datetime as dt

v = SoldModelInfo.cell(1,7).value

try:
    d = dt.datetime.strftime(xldate_to_datetime(v), '%Y-%m-%d')
except TypeError as e:
    print('Could not parse "{}": {}'.format(v, e)

Python break, continue and pass Statements, So I want to get: 0,1,2,20,5,6,7,8, here I skip the fourth iteration. for r in R: if r == 3: a = next(R) # remove the next element from the iterator print(r) The question is a pure python question, consider reviewing data structures� continue passes control to the next iteration of a for or while loop. It skips any remaining statements in the body of the loop for the current iteration. The program continues execution from the next iteration. continue applies only to the body of the loop where it is called.

How to skip a single loop iteration, Skipping the first element of a for-loop iterates over all elements of an iterable except the first. For example, iterating over the list [0, 1, 2, 3] and skipping the first � Simple For Loop in Python. Output: 10 12 15 18 20. From the example above, we can see that in Python’s for loops we don’t have any of the sections we’ve seen previously. There is no initializing, condition or iterator section.

How to skip the first element of a for-loop in Python, In Python, an assignment statement can make two variables equal, but Then the assignment is made, so that the variable on the left-hand side now refers to the you have to initialize it to some starting value, usually with a simple assignment: skip the processing of the rest of the body of the loop, for the current iteration. break exits the loop, while continue just skips the rest of the current iteration and then continues (hence the name). – Efferalgan Oct 5 '16 at 5:59 I think you are looking for continue to skip the current iteration of the loop – cricket_007 Oct 5 '16 at 5:59

7. Iteration — How to Think Like a Computer Scientist: Learning with , continue labelname;. The continue statement (with or without a label reference) can only be used to skip one loop iteration. The break� Hi, i have a for-loop and at the end of each iteration I want to check if a certain condition holds, if so, the loop is supposed to skip the next iteration. I tried it the (for me) obvious way: for j=1:h

Comments
  • Chunk under the except after continue isn't even reachable?
  • Your indentation looks off. You field extractions should be after the except KeyError, not part of it. You also need to catch (and apparently ignore) errors for each assignment separately.
  • Sorry I have code of over 500 lines so it was a pain to provide a sample of it. I would need to do a try-except for every single variable? I guess that isnt a bad solution I just didnt know if that was what it would take. Also, the code under the except IS reachable. I have ran this code many times and it works. If it hits the except it goes to next loop, if not it goes to variable extraction.
  • Are your data all consisted of one row within the sheet? It's a bit confusing in your example as each iteration is per file not per row in the xls. Because my next question would be if they were in multiple rows, if one row has erroneous data is the entire row thrown out or just skip the particular error value.
  • I think what @chepner is saying regarding your indentation is that the SoldModelInfo parts should be unindented one level, because once your code hits continue it would no longer execute the codes under SoldModelInfo. Possibly in your code it is indented correctly just not in your sample.
  • Would this have any performance advantage compared to multiple try-excepts?
  • No. In essence it's doing the same thing, and if you are mostly passing in one function at a time you would need to update the try_funcs to not use reduce each time. This just helps tidy up your code without all the try blocks and help provide a default value in the same line.
  • Is there a way to append the variable name that I am setting the try_funcs function to? Ie. could I append "ExpirationDate" automatically if it had an exception when the try_funcs function is called?
  • Not automatically without jumping through some hoops (I'd imagine inspect module can come in handy but I'm not sure if it'd work or is worth the trouble)... probably better to just update try_funcs to also include the variable name as a string if that matters to you, then just log the variable name as part of the except clause. try_funcs('ExpirationDate', SoldModelInfo.cell(1,7).value), "some default", [some_funcs, ...])
  • Here's a relevant question regarding inspecting the variable name if you're interested: stackoverflow.com/questions/8875202/…
  • xldate_to_datetime is a proprietary function I have created. I am not using the one from xlrd.
  • @Coldchain9 I don't understand what you mean. What exception occurs when date is in wrong format?
  • I believe the error is coming from Excel because the user typed in '9/31/2015' so Excel doesnt recognize it as a date thus sends it in as a string instead of a float date which is the usual case when extracting an xldate. TypeError: unsupported type for timedelta days component: str from function def xldate_to_datetime(xldate): temp=dt.datetime(1899,12,30) delta=dt.timedelta(days=xldate) return temp+delta
  • Ok, I updated the sample code. Instead of ValueError you need to catch TypeError