2

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
8
  • Which line of code does it return the error on? Dec 31 '14 at 19:13
  • How did you define the variables civicAddress and flds?
    – Aaron
    Dec 31 '14 at 19:20
  • I had some exception handling that was masking the location of the error, but when I remove that the line it fails on is rows.updateRow(row)which appears inline with the for loop. Dec 31 '14 at 19:20
  • civicAddress = ("{0}\{1}".format(outputGDB, "CIVIC_ADDRESS")) Dec 31 '14 at 19:20
  • flds = ("STREET_NUMBER_PREFIX", "STREET_DIR_PREFIX", "STREET_NUMBER_SUFFIX", "CIVIC_ID") Dec 31 '14 at 19:21
3

If you want to do updates with your UpdateCursor, you'll need to move rows.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
1
  • This actually worked for me, it was the incorrect indentation of rows.updateRow(row) that was not allowing iteration. Thanks! Dec 31 '14 at 21:06
2

I bet the issue stems from mixing UpdateCursors with InsertCursors. Instead of having both processing simultaneously, I suggest separating the two.

Create an empty list prior to either.

li = []

Remove your InsertCursor from the start of the script, and any newrow = insertCursor.newRow() should also be removed. Then, replace all your insertCursor.insertRow(newrow) with li.append ((row[x], row[y])), where x and y are the appropriate indexes.

Finally, once your UpdateCursor is complete, perform your InsertCursor with rows from your list.

insertCursor =  arcpy.da.InsertCursor(subAddress, flds2)
for rw in li:
    insertCursor.insertRow(rw)
2
  • This is good advice. And if the target of both cursors is the same data source, you'll need to be in an editing session. see first note here
    – Mintx
    Dec 31 '14 at 20:29
  • Thanks. I think your simple answer is correct however! Dec 31 '14 at 20:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.