AnsweredAssumed Answered

For Loop not looping correctly, ArcPy

Question asked by mjazemar on Feb 1, 2018
Latest reply on Feb 2, 2018 by mjazemar

I have four markets 1-4, but I only want to do markets 2 and 3. I created a markets list only containing those two markets so that I could use the list in a "for" loop. However, when I try and run it, it still runs through all the markets. I think this has to do with my "whereclause" or "sqlclause" or both, but I can't figure out how to fix it. I've tried using a wildcard in the "whereclause" but that doesn't work. I've also tried getting rid of the "whereclause" but that also doesn't work. Could someone help me with this?

 

[Code]-

fc = 'Centers'
fields = ['market_id','Center_ID','Center_Name','Opening_Date']
fieldname = 'market_id'

#Define WHERE clause statement
#whereclause = """{} = '%000' """.format(arcpy.AddFieldDelimiters(fc, fieldname))

sqlclause = (None, 'Order By market_id, Center_ID')

# Set Markets to loop through
markets = [2,3]
# Set Years to loop through
years = [2016,2017]
# Set Months to loop through
months = [1, 2]

#returns last day of each month
def last_day_of_month(any_day):
    next_month = any_day.replace(day=28) + datetime.timedelta(days=4)
    return next_month - datetime.timedelta(days=next_month.day)

for market in markets:
    sqlclause = (None, 'Order By market_id, Center_ID')
    print (market)
    for year in years:
        print (year)
        for month in months:
            print (month)
            with arcpy.da.SearchCursor(in_table = fc, field_names = fields, where_clause=whereclause, sql_clause=(None, 'ORDER BY market_id, Center_ID')) as cursor:

Outcomes