mlou

Cursor within a cursor

Discussion created by mlou on Dec 20, 2011
Latest reply on Dec 20, 2011 by mlou
I have a script that creates a few datasets, and at the end of the script I get it to create a table with 5 fields, then run a search cursor through one of the new datasets and try to copy the unique values to the new table (basically the first record with a value in a specific field - all other records with that value are skipped).

To do this I created a search cursor to step through the feature class, and for each record use another search cursor to step through the table to see if the field value exists.  If it doesn't it uses an insert cursor to add the record to the table, if it does exist it steps to the next record in the feature class.

The first search cursor works, however it seems to skip the second cursor completely (sometimes it hits the second cursor only once, sometimes it skips it completely, sometimes python crashes).  The insert cursor doesn't always work either (if it skips the second search cursor it should write every record from the feature class into the table).

Is there an easier way to do this, or if not, what am I doing wrong in my script?

Here's the part of the script that isn't working
# Create Table to track Hydrants to test
print "# Create Table to track Hydrants to test"
arcpy.CreateTable_management(HT_SCRATCH_GDB, HydrantTestTable, "", "")

# Add fields to table
print "# Add fields to table"
arcpy.AddField_management(HT_SCRATCH_GDB + "\\" + HydrantTestTable, "COMPKEY", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(HT_SCRATCH_GDB + "\\" + HydrantTestTable, "UNITID", "TEXT", "", "", "16", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(HT_SCRATCH_GDB + "\\" + HydrantTestTable, "AREA", "TEXT", "", "", "5", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(HT_SCRATCH_GDB + "\\" + HydrantTestTable, "TOTEST", "TEXT", "", "", "2", "", "NULLABLE", "NON_REQUIRED", "")
arcpy.AddField_management(HT_SCRATCH_GDB + "\\" + HydrantTestTable, "NEAR_FID", "LONG", "", "", "", "", "NULLABLE", "NON_REQUIRED", "")

print "# Start search cursors"
x = 0
y = 0
counter = 0

# Refresh to try to force search cursor to work
arcpy.RefreshCatalog(HT_SCRATCH_GDB)
arcpy.RefreshCatalog(HydrantTestTablePath)

hs_rows = arcpy.SearchCursor(HydrantSnapped, "","","", "NEAR_FID A")

# Insert dummy record to try to force search cursor to work
htt_ins_rows = arcpy.InsertCursor(HydrantTestTablePath)
htt_ins_row = htt_ins_rows.newRow()
htt_ins_row.COMPKEY = 1
htt_ins_row.UNITID = "DummyRecord"
htt_ins_row.AREA = "1"
htt_ins_row.ToTest = "N"
htt_ins_row.NEAR_FID = 0
htt_ins_rows.insertRow(htt_ins_row)

del htt_ins_row
del htt_ins_rows

htt_ins_rows = arcpy.InsertCursor(HydrantTestTablePath)

htt_rows = arcpy.SearchCursor(HydrantTestTablePath, "", "", "", "NEAR_FID A")

for hs_row in hs_rows:
    NFID_Exists = 0
    x = x+1

    for htt_row in htt_rows:
        y = y+1
        print "y = "+ str(y)
        if htt_row.NEAR_FID == hs_row.NEAR_FID:
            NFID_Exists = 1
            break
    if NFID_Exists == 0:
        print "adding record"
        counter = counter+1
        htt_ins_row = htt_ins_rows.newRow()
        htt_ins_row.COMPKEY = hs_row.COMPKEY
        htt_ins_row.UNITID = hs_row.UNITID
        htt_ins_row.AREA = hs_row.AREA
        htt_ins_row.ToTest = "Y"
        htt_ins_row.NEAR_FID = hs_row.NEAR_FID
        htt_ins_rows.insertRow(htt_ins_row)

del htt_row
del htt_rows
del hs_row
del hs_rows
thanks,
Mike.

Outcomes