My arcpy script includes two updateCursor Objects. The first iteration of the "outer" coursor works fine, but the programm stops when it comes to the second iteration at updateRow(). Has somebody an idea, where the problem could be?

Here is the code snippet:

arcpy.env.workspace = "I:/GIS/VoGIS_Projekte/Wanderwege/Basisdaten/Basisdaten_Sommer/wandern_SK.gdb"

# Use row object to get and set field values
cursor = arcpy.UpdateCursor("wanderwege/wander_wege_3d")

# Iterate through rows and update values
row_i = 1
wa_j = 1
for row in cursor:
    print row_i
    row.WEG_LAENGE = '999'
    cursor.updateRow(row)               # The 2nd iteration stops here...
    wwege = arcpy.UpdateCursor("wanderwege/wander_wege")

    for wa in wwege:
        print wa_j
        wa.WEG_KATEGO = "Kaps999"
        wwege.updateRow(wa)                 # save
        wa_j = wa_j + 1
    del wwege
    del wa

    row_i = row_i + 1

del row
del cursor

I am working with: ArcGIS 10.2 Python 2.7 Windows 7

P.S.: I know, that this code doesn't make much sense - I just wanted to reproduce the error of a quite complicated script.

  • 1
    Would you be able to edit your question to include just a code snippet that shows this error, please? – PolyGeo Jan 9 '15 at 9:51
  • 1
    A code sample is required with a coding question, but you need to break it down into the smallest code block that exhibits the behavior. Failure to do so will result in downvotes and "too long, didn't read" responses. – Vince Jan 9 '15 at 12:07
  • I have now changed the code snippet. – Stefan Jan 13 '15 at 7:52
  • Have you tried it with arcpy.da cursors, which are reported to nest better? gis.stackexchange.com/questions/74325/arcpy-nested-loop-problem – phloem Jan 13 '15 at 18:12
  • Strange: the problem does NOT appear when using shapefiles. – Stefan Jan 14 '15 at 13:02

After a lot of testing, I have found out, where the problem is:

  1. nested loops with "old" cursors (not data access cursors) and with geodatabases are making troubles - nested loops, "old" cursors and shapefiles are working normally.

  2. when using nested loops with da.cursors, you have to open an edit session with da.editor - this is just for nested loops relevant.

Thank you "phloem" for the hint.


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.