4

I have a question connected with ArcPy. I was trying to make a nested loop with 2 cursors - 1st with 2 elements and 2nd with 5 elements. The statement should return 10 connections, e.g. 1-1, 1-2, ... 2-5. Here is part of my code:

import arcpy
import os.path

folder = "Z:\WOLGA"    
na="na"    
baza = "testy.mdb"    
dataset = "network_analyst"    
arcpy.env.workspace = os.path.join(folder, na, baza, dataset)    
shapePocz = os.path.join(folder, na, baza, dataset, "dom")

listaPktPocz = arcpy.SearchCursor(shapePocz, "", "", "", "")    
shapeKonc = os.path.join(folder, na, baza, dataset, "punkty_docelowe")    
listaPktKonc = arcpy.SearchCursor(shapeKonc, "", "", "", "")    
shapeKoncName = arcpy.Describe(shapeKonc).shapeFieldName    
shapePoczName = arcpy.Describe(shapePocz).shapeFieldName

for pktPocz in listaPktPocz:
    for pktKonc in listaPktKonc:
        print str(pktPocz.numer) + " - " + str(pktKonc.numer)

For now I got such connections as: 1-1, 1-2, 1-3, 1-4, 1-5 only. The outer loop is not being executed properly.

1
  • What version of ArcGIS for Desktop are you using? Knowing this detail is often important because ArcPy 10.1 had a number of significant features added to it, and it is useful to know what we have to work with. Just edit it into your original question.
    – PolyGeo
    Commented Oct 15, 2013 at 8:34

3 Answers 3

6

The problem is definitely the old-type cursor. It often makes some problems with nested loops. I recommend you to use da.SearchCursor. The code should look like this:

with arcpy.da.SearchCursor(shapePocz, ["numer"]) as searchCursOuter:
    for pktPocz in searchCursOuter:
        with arcpy.da.SearchCursor(shapeKonc, ["numer"]) as searchCursInner:
            for pktKonc in searchCursInner:
                print str(pktPocz[0]) + " - " + str(pktKonc[0])

I bet it works.

0
3

The problem may be that the cursors aren't being properly closed/deleted. Whenever possible you should use the new Data Access cursors: arcpy.da.SearchCursor, arcpy.da.UpdateCursor, and arcpy.da.InsertCursor. You can use a 'with' block, which will automatically close and cleanup your cursors for you:

with arcpy.da.SearchCursor(listaPktPocz, list_fields=[fieldNameOuter]) as searchCursOuter:
    with arcpy.da.SearchCursor(listaPktKonc, list_fields=[fieldNameInner]) as searchCursInner:
        for pktPocz in searchCursOuter:
            for pktKonc in searchCursInner:
                print str(pktPocz[0]) + " - " + str(pktKonc[0])

See the following links:

Accessing data using cursors (pay attention to the arcpy.da cursors)

Search Cursor (arcpy.da)

1
  • 4
    Much better to use with statements. However, they don't work with "normal" cursors and the data access module is only available @ 10.1 and above. (+1)
    – Paul
    Commented Oct 14, 2013 at 16:19
1

Since you are using the old style cursor which is not compatible with with statements, you need to call a reset() on your inner loop SearchCursor inside the outer loop. Since the inner loop SearchCursor has already been iterated, you get nothing on your second pass through the inner loop.

for pktPocz in listaPktPocz:
    for pktKonc in listaPktKonc:
        print str(pktPocz.numer) + " - " + str(pktKonc.numer)
    listaPktKonc.reset()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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