AnsweredAssumed Answered

Another da.SearchCursor Issue

Question asked by jborgion on Nov 15, 2017
Latest reply on Nov 15, 2017 by jborgion

Just when I thought I had cursors figured out....

 

I have a small test table (4 records) that I apply arcpy.MakeTableView_management to.  Then I run a simple search cursor:

 

>>> fields = ["Type","StreetName"]
>>> with arcpy.da.SearchCursor("view",fields) as cursor:
...     for row in cursor:
...         if "'{}'".format(row[0]) == "'{}'".format("Junk"):
...            print "'{},{}'".format(row[1],row[0])
...           
'BUCKMAN,Junk'

The print statement returns the one row that meets the conditional.  Cool....

 

But I want to select that one single record.  If I swap out the print statement with :

 

arcpy.SelectLayerByAttribute_management("view","NEW_SELECTION")

 All 4 records get selected.  In fact if I run it like this:

 

>>> with arcpy.da.SearchCursor("view",fields) as cursor:
...     for row in cursor:
...         if "'{}'".format(row[0]) == "'{}'".format("Junk"):
...            print "'{},{}'".format(row[1],row[0])
...            arcpy.SelectLayerByAttribute_management("view","NEW_SELECTION")
...           
'BUCKMAN,Junk'

You can see it prints just the fields from the record that meets the conditional, but all four records get selected.  It's gotta be a (simple) Joe thing, but I'm missing it....

 

Dan Patterson

Joshua Bixby

Outcomes