arcpy.da.UpdateCursor skipping every other record (but kudos for speed)

423
2
Jump to solution
01-10-2012 02:20 PM
RebeccaStrauch__GISP
MVP Emeritus
I've created a 10.1 arcpy.da cursor "feeding-in" 13 fields.  I then process the records with a "for theRow in theRows:" with a series of if, elif statements that then assign a code to one field based on the results.  I was using the same if/elif logic that works great in 10.0. but with the new "theRow[]" syntax. 

The code runs thru, but every other record is <null> even though at the very least it should have a value of 9000 which is in my "else:" statement. 

I thought maybe the "theRow = theRows.next()" statement at the end may have caused the problem, but removing these did not help (nor did it seem to hurt).

Anyone have any idea why it would work on every other record? 

BTW - Kudos for the increase speed of both cursors, TableToTable_management, and Joinfield_management in python.  This script used to take over 5 hours, now only 11 minutes...the skipped records not withstanding.  Actually the T2T and JoinField had the most improvement in my script!  thanks!


    theRow, theRows = None, None     theRows = arcpy.da.UpdateCursor(UCU_arc, ['LEFT_FID', 'RIGHT_FID', 'LUCU', 'RUCU',                                                'LGeoDetail', 'RGeoDetail', 'LUnitSub', 'RUnitSub',                                                'LSubMinor', 'RSubMinor', 'LMinorSpecific', 'RMinorSpecific',                                               'LineID'])      #    The values to be using the the 10.1 cursor: #            LEFT_FID and RIGHT_FID:  theRow[0], theRow[1] #      LUCU and RUCU:  theRow[2], theRow[3] #      LGeoDetail and RGeoDetail:  theRow[4], theRow[5] #      LUnitSub and RUnitSub:  theRow[6], theRow[7] #      LSubMinor and RSubMinor:  theRow[8], theRow[9] #      LMinorSpecific and RMinorSpecific:  theRow[10], theRow[11] #      LineID : theRow[12]          # ArcGIS 10.0 version.... #    theRow = theRows.next() #    for theRow in theRows: #        if ((theRow.LEFT_FID == -1) or (theRow.RIGHT_FID == -1)): #            theRow.LineID = 6000 #            theRows.updateRow(theRow) #        elif ((theRow.LUCU == theRow.RUCU) and (theRow.LGeoDetail == 5 or theRow.RGeoDetail == 5)): #            theRow.LineID = 4000 #            theRows.updateRow(theRow) #        elif ((theRow.LUCU == theRow.RUCU) and (theRow.LGeoDetail == 9 or theRow.RGeoDetail == 9)): #            theRow.LineID = 4000 #            theRows.updateRow(theRow) #        elif ((theRow.LUCU == theRow.RUCU) and (theRow.LGeoDetail == 6 or theRow.RGeoDetail == 6)): #            theRow.LineID = 5000 #            theRows.updateRow(theRow) #        elif (theRow.LUnitSub != theRow.RUnitSub): #            theRow.LineID = 1000 #            theRows.updateRow(theRow) #        elif (theRow.LSubMinor != theRow.RSubMinor): #            theRow.LineID = 2000 #            theRows.updateRow(theRow) #        elif ((theRow.LSubMinor == theRow.RSubMinor) and (LMinorSpecific != RMinorSpecific)): #            theRow.LineID = 3000 #            theRows.updateRow(theRow) #        else: #            theRow.LineID = 9000 #            print LineID, LUCU, RUCU #        theRow = theRows.next()          #theRow = theRows.next()     for theRow in theRows:  if ((theRow[0] == -1) or (theRow[1] == -1)):      theRow[12] = 6000      theRows.updateRow(theRow)  elif ((theRow[2] == theRow[3]) and (theRow[4] == 5 or theRow[5] == 5)):      theRow[12] = 4000      theRows.updateRow(theRow)  elif ((theRow[2] == theRow[3]) and (theRow[4] == 9 or theRow[5] == 9)):      theRow[12] = 4000      theRows.updateRow(theRow)  elif ((theRow[2] == theRow[3]) and (theRow[4] == 6 or theRow[5] == 6)):      theRow[12] = 5000      theRows.updateRow(theRow)  elif (theRow[6] != theRow[7]):      theRow[12] = 1000      theRows.updateRow(theRow)  elif (theRow[8] != theRow[9]):      theRow[12] = 2000      theRows.updateRow(theRow)  elif ((theRow[8] == theRow[9]) and (theRow[10] != theRow[11])):      theRow[12] = 3000      theRows.updateRow(theRow)  else:      theRow[12] = 9000      print theRow[12], theRow[4], theRow[5]              #finally:         # Regardless of whether the script succeeds or not, delete          #  the row and cursor         #     if theRow:         del theRow     if theRows:         del theRows
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JasonScheirer
Occasional Contributor III
Your else isn't actually calling updateRow:

 else:      theRow[12] = 9000      print theRow[12], theRow[4], theRow[5]


Another thing you can do now so you don't have to insert all those manual del statements:

with arcpy.da.UpdateCursor(UCU_arc, ['LEFT_FID', 'RIGHT_FID', 'LUCU', 'RUCU',                                                'LGeoDetail', 'RGeoDetail', 'LUnitSub', 'RUnitSub',                                                'LSubMinor', 'RSubMinor', 'LMinorSpecific', 'RMinorSpecific',                                               'LineID']) as theRows:     for theRow in theRows:          <insert code>


Once the code leaves the with block, whether it errors or not, it will close the cursor and release any outstanding locks. You don't need to delete theRow since it's a list and not a row object in arcpy.da, so it's not tied to the cursor/cursor's lock.

View solution in original post

0 Kudos
2 Replies
JasonScheirer
Occasional Contributor III
Your else isn't actually calling updateRow:

 else:      theRow[12] = 9000      print theRow[12], theRow[4], theRow[5]


Another thing you can do now so you don't have to insert all those manual del statements:

with arcpy.da.UpdateCursor(UCU_arc, ['LEFT_FID', 'RIGHT_FID', 'LUCU', 'RUCU',                                                'LGeoDetail', 'RGeoDetail', 'LUnitSub', 'RUnitSub',                                                'LSubMinor', 'RSubMinor', 'LMinorSpecific', 'RMinorSpecific',                                               'LineID']) as theRows:     for theRow in theRows:          <insert code>


Once the code leaves the with block, whether it errors or not, it will close the cursor and release any outstanding locks. You don't need to delete theRow since it's a list and not a row object in arcpy.da, so it's not tied to the cursor/cursor's lock.
0 Kudos
RebeccaStrauch__GISP
MVP Emeritus
Thanks Jason. I modified using your suggestions and it's no longer skiping a record.  In fact, I can't even duplicate the problem this morning if I change the code back.  So something in there in my original code must have been causing problems.

Good catch on the "else" missing the updateRow.  It shouldn't even make it to the else, but it does still need it. Just an oversite on my part.  This still doesn't explain the every other line, but since it's working now I won't worry about it. Maybe in connection with the "del" statements, although again it shouldn't have hit that until the end.

Anyway, back up and running I think.  Just need to tweak my "if"s and verify it the same results as 10.0.

Thanks!
0 Kudos