Select to view content in your preferred language

Find out how many rows are selected

7420
2
04-26-2011 08:25 AM
JeffGrice
New Contributor
How do I get a count of the selected features in a Layer using python?
0 Kudos
2 Replies
FrankVignati
Frequent Contributor
How do I get a count of the selected features in a Layer using python?


use the getcount
you have to change the getcount result to an integer:

ALyr = arcpy.mapping.ListLayers(IMXD,"address_point")[0]
arcpy.SelectLayerByAttribute_management(ILyr, "NEW_SELECTION", SheetQry)

## Test for slected address points
NumS = str(arcpy.GetCount_management(ALyr))
Num = int(NumS)
print Num


or a string:

   arcpy.SelectLayerByAttribute_management(ILyr, "NEW_SELECTION", SheetQry)
   Num = str(arcpy.GetCount_management(ILyr))
   if Num == "0":
       arcpy.AddMessage("!! NO CORRESPONDING PLSS AREA EXISTS FOR THIS REQUEST !!")
0 Kudos
GerickeCook
Deactivated User
Thank you, Frank.

I was having trouble with the result from the getcount method and using it in an if-then statement. I thought that I would post my issue in case any one else encounters the same problem. This was a section of my code:
count = arcpy.GetCount_management("lyr")
#Need min count for next tool
if count > 5:
   print count
   arcpy.CollectEvents_stats("lyr", eventslyr)


It was always evaluating to greater than 5, even if I had print statements that showed that the count was 2. Your suggestion fixed it for me. I know realize that the count variable stores something like an object rather than an integer.  This was the fix for me.

count = int(str(arcpy.GetCount_management("lyr")))
#Need min count for next tool
if count > 5:
   print count
   arcpy.CollectEvents_stats("lyr", eventslyr)