get number records returned by 2nd cursor

2998
2
Jump to solution
12-09-2015 12:01 PM
SkipRepetto
New Contributor II

So I want to get the number of records in  table1 returned first by an UpdateCursor and then in table2 get the number of records returned by a SearchCursor. My code uses GetCount_management and works fine getting the number of rows returned for the first table. For the  second table,  I try this same process, but I am not getting the number of selected records, I get the TOTAL number of records in the table which I dont care about.  I know I've seen this happen before but can't remember why it's happening. Thanks in advance for the help.

with arcpy.da.UpdateCursor(inPtMeasureTbl,("STREAM_CODE","SPECSTR","TYPE","MEAS","PROB"), "TYPE = 'MIDSP' " ) as mainCursor:
    result = arcpy.GetCount_management(inPtMeasureTbl)
    count = int(result.getOutput(0))
    print count #this works fine
    
    for curRec in mainCursor:
        #print "Working on AWC_CODE: %s  Specstr: %s  Meas: %f" % (curRec[0], curRec[1], curRec[3])            
        #Compare to # of line events occurring at this spot
        srchExpr = "STREAM_CODE='%s' and F_MEAS <= %f and T_MEAS >= %f" % (curRec[0], curRec[3],curRec[3] )
        with arcpy.da.SearchCursor(inEventTbl,("STREAM_CODE","SPECLS","F_MEAS","T_MEAS"), srchExpr) as lkupCursor:              
            numLineEvents = int(arcpy.GetCount_management(inEventTbl).getOutput(0))
            print numLineEvents #THIS NUMBER IS THE TOTAL WHICH IS WRONG

            for curEventTbl in lkupCursor:
                print "Found event: %s  Specstr: %s  Measures: %f , %f" % (curEventTbl[0],curEventTbl[1], curEventTbl[2], curEventTbl[3])
0 Kudos
1 Solution

Accepted Solutions
FreddieGibson
Occasional Contributor III

I would expect the second issue you're seeing because the creation of the cursor has no effect on the source data. I would also assume that the count you're getting for the first cursor is wrong unless your data happens to match the filter in the cursor. Have you tried creating a feature layer against the data using the filters you're providing to the cursor? Otherwise you can iterate through the cursor and get the count prior to looping through it again. I wrote up examples of this below, but note I didn't test either of these. They're just how I'm thinking this would work.

Option A - Get the count using a table view or feature layer

ufields = ("STREAM_CODE","SPECSTR","TYPE","MEAS","PROB")
ufilter = "TYPE = 'MIDSP'"
with arcpy.da.UpdateCursor(inPtMeasureTbl, ufields, ufilter) as ucursor:
    utview = arcpy.management.MakeTableView(inPtMeasureTbl, "utable", ufilter)
    ucount = int(arcpy.management.GetCount(uview).getOutput(0))
    for urow in ucursor:
        cfields = ("STREAM_CODE","SPECLS","F_MEAS","T_MEAS")
        cfilter =  "STREAM_CODE='{}' and F_MEAS <= {:.6f} and T_MEAS >= {:.6f}".format(*[urow[0], urow[3], urow[3]])
        with arcpy.da.SearchCursor(inEventTbl, cfields, cfilter) as ccursor:
            ctview = arcpy.management.MakeTableView(inEventTbl, "ctable", cfilter)
            ccount = int(arcpy.management.GetCount(ctview).getOutput(0))
            print("Line Events Count: {0}".format(count))
            
            for crow in ccursor:
                print("Found event: {} Specstr: {} Measures: {:.6f} , {:.6f}".format(*row))

Option B - Get the count from the cursor

ufields = ("STREAM_CODE","SPECSTR","TYPE","MEAS","PROB")
ufilter = "TYPE = 'MIDSP'"
with arcpy.da.UpdateCursor(inPtMeasureTbl, ufields, ufilter) as ucursor:
    ucount = 0
    for _ in ucursor:
        ucount += 1    
    ucursor.reset()
    
    for urow in ucursor:
        cfields = ("STREAM_CODE","SPECLS","F_MEAS","T_MEAS")
        cfilter =  "STREAM_CODE='{}' and F_MEAS <= {:.6f} and T_MEAS >= {:.6f}".format(*[urow[0], urow[3], urow[3]])
        with arcpy.da.SearchCursor(inEventTbl, cfields, cfilter) as ccursor:
            ccount = 0
            for _ in ccursor:
                ccount += 1                
            ccursor.reset()
            
            print("Line Events Count: {0}".format(count))
            
            for crow in ccursor:
                print("Found event: {} Specstr: {} Measures: {:.6f} , {:.6f}".format(*row))

View solution in original post

2 Replies
FreddieGibson
Occasional Contributor III

I would expect the second issue you're seeing because the creation of the cursor has no effect on the source data. I would also assume that the count you're getting for the first cursor is wrong unless your data happens to match the filter in the cursor. Have you tried creating a feature layer against the data using the filters you're providing to the cursor? Otherwise you can iterate through the cursor and get the count prior to looping through it again. I wrote up examples of this below, but note I didn't test either of these. They're just how I'm thinking this would work.

Option A - Get the count using a table view or feature layer

ufields = ("STREAM_CODE","SPECSTR","TYPE","MEAS","PROB")
ufilter = "TYPE = 'MIDSP'"
with arcpy.da.UpdateCursor(inPtMeasureTbl, ufields, ufilter) as ucursor:
    utview = arcpy.management.MakeTableView(inPtMeasureTbl, "utable", ufilter)
    ucount = int(arcpy.management.GetCount(uview).getOutput(0))
    for urow in ucursor:
        cfields = ("STREAM_CODE","SPECLS","F_MEAS","T_MEAS")
        cfilter =  "STREAM_CODE='{}' and F_MEAS <= {:.6f} and T_MEAS >= {:.6f}".format(*[urow[0], urow[3], urow[3]])
        with arcpy.da.SearchCursor(inEventTbl, cfields, cfilter) as ccursor:
            ctview = arcpy.management.MakeTableView(inEventTbl, "ctable", cfilter)
            ccount = int(arcpy.management.GetCount(ctview).getOutput(0))
            print("Line Events Count: {0}".format(count))
            
            for crow in ccursor:
                print("Found event: {} Specstr: {} Measures: {:.6f} , {:.6f}".format(*row))

Option B - Get the count from the cursor

ufields = ("STREAM_CODE","SPECSTR","TYPE","MEAS","PROB")
ufilter = "TYPE = 'MIDSP'"
with arcpy.da.UpdateCursor(inPtMeasureTbl, ufields, ufilter) as ucursor:
    ucount = 0
    for _ in ucursor:
        ucount += 1    
    ucursor.reset()
    
    for urow in ucursor:
        cfields = ("STREAM_CODE","SPECLS","F_MEAS","T_MEAS")
        cfilter =  "STREAM_CODE='{}' and F_MEAS <= {:.6f} and T_MEAS >= {:.6f}".format(*[urow[0], urow[3], urow[3]])
        with arcpy.da.SearchCursor(inEventTbl, cfields, cfilter) as ccursor:
            ccount = 0
            for _ in ccursor:
                ccount += 1                
            ccursor.reset()
            
            print("Line Events Count: {0}".format(count))
            
            for crow in ccursor:
                print("Found event: {} Specstr: {} Measures: {:.6f} , {:.6f}".format(*row))
SkipRepetto
New Contributor II

Thanks Freddie,

your first sentence was indeed fundamental to my problem:
"I would expect the second issue you're seeing because the creation of the cursor has no effect on the source data. I would also assume that the count you're getting for the first cursor is wrong unless your data happens to match the filter in the cursor."  I tested out that theory and indeed, the cursor has no effect on source data.

I had already implemented a manual counter as a work around - your "Option B" which of course works.

0 Kudos