More

Arcpy.da cursor iteration not started?


I am trying to convert cursors written in 10.0 to use the new arcpy.da module in 10.2.1. If I step through the code in debug mode, it steps into the for loop; however, when I attempt to run the code I get an exception : iteration not started

code for 10.0

for row in updateCursor: if row.getValue("STREET_NUMBER_PREFIX") in ['N', 'S', 'W']: row.setValue("STREET_DIR_PREFIX", row.getValue("STREET_NUMBER_PREFIX")) row.setValue("STREET_NUMBER_PREFIX", None)

code for 10.2.1

with arcpy.da.UpdateCursor(civicAddress, flds) as rows: for row in rows: if row[0] in ['N', 'S', 'W']: row[1] = row[0] row[0] = None

What can I do to solve my problem?

I have quite a few code blocks as above that deal with specific logic cases.

Here is the full code for that section, maybe it will help:

flds = ("STREET_NUMBER_PREFIX", "STREET_DIR_PREFIX", "STREET_NUMBER_SUFFIX", "CIVIC_ID") flds2 = ("CIVIC_ID", "UNIT_NUMBER") log.information ("Creating cursors… ") insertCursor = arcpy.da.InsertCursor(subAddress, flds2) #row = [] with arcpy.da.UpdateCursor(civicAddress, flds) as rows: for row in rows: if row[0] in ['N', 'S', 'W']: row[1] = row[0] row[0] = None if row[2] in ['N', 'S', 'W']: row[1] = row[2] row[2] = None if row[2] in unitLst: newrow = insertCursor.newRow() newrow[0] = row[3] newrow[1] = row[2] row[2] = None insertCursor.insertRow(newrow) if row[0] in unitLst: newrow = insertCursor.newRow() newrow[0] = row[3] newrow[1] = row[0] row[0] = None insertCursor.insertRow(newrow) try: if re.match("[0-9][A-Z]", row[0]) or re.match("[0-9][0-9][A-Z]", row[0]) or  re.match("[A-Z][0-9]", row[0]) or re.match("[A-Z][0-9][0-9]", row[0]): newrow = insertCursor.newRow() newrow[0] = row[3] newrow[1] = row[0] row.setValue("STREET_NUMBER_PREFIX", None) row[0] = None insertCursor.insertRow(newrow) except: pass try: if re.match("[0-9][A-Z]", row[2]) or re.match("[0-9][0-9][A-Z]", row[2]) or  re.match("[A-Z][0-9]", row[2]) or re.match("[A-Z][0-9][0-9]", row[2]): newrow = insertCursor.newRow() newrow[0] = row[3] newrow[1] = row[2] row[2] = None insertCursor.insertRow(newrow) except: pass try: if row[0].isdigit(): newrow = insertCursor.newRow() newrow[0] = row[3] newrow[1] = row[0] row.setValue("STREET_NUMBER_PREFIX", None) row[0] = None insertCursor.insertRow(newrow) except: pass try: #if row.getValue("STREET_NUMBER_SUFFIX").isdigit(): if row[2].isdigit(): newrow = insertCursor.newRow() #newrow.setValue("CIVIC_ID", row.getValue("CIVIC_ID")) newrow[0] = row[3] # newrow.setValue("UNIT_NUMBER", row.getValue("STREET_NUMBER_SUFFIX")) newrow[1] = row[2] #row.setValue("STREET_NUMBER_SUFFIX", None) row[2] = None insertCursor.insertRow(newrow) except: pass rows.updateRow(row) del row del newrow del rows

If you want to do updates with your UpdateCursor, you'll need to moverows.updateRow(row)inside your for loop:

with arcpy.da.UpdateCursor(civicAddress, flds) as rows: for row in rows:… snip… rows.updateRow(row) #needs to be indented inside loop del row del newrow del rows

I bet the issue stems from mixingUpdateCursorswithInsertCursors. Instead of having both processing simultaneously, I suggest separating the two.

Create an empty list prior to either.

li = []

Remove yourInsertCursorfrom the start of the script, and anynewrow = insertCursor.newRow()should also be removed. Then, replace all yourinsertCursor.insertRow(newrow)withli.append ((row[x], row[y])), where x and y are the appropriate indexes.

Finally, once yourUpdateCursoris complete, perform yourInsertCursorwith rows from your list.

insertCursor = arcpy.da.InsertCursor(subAddress, flds2) for rw in li: insertCursor.insertRow(rw)