I'm trying to only return information on a selected feature in a shapefile. According to other answers online, searchcursors should only act on selected features, but for whatever reason it's providing me with all the features in the shapefile.

Here's a basic example of what I'm working with:

for row in arcpy.SearchCursor(shapefile):
    print row.TEXT

This code prints the output from the "TEXT" field for all features, not only the one I have selected.

  • 1
    Welcome to GIS SE! As a new user please take the tour to learn about our focused Q&A format. Please edit your question to include your ArcGIS version and more of your code, including the definition of shapefile. – Midavalo Feb 16 '17 at 21:06
  • How are you selecting the features? – Aaron Feb 17 '17 at 6:46
  • Can you post your entire code? I can't reproduce the problem using the snippet you've provided (i.e. select features, run your code, returns only selected features). – phloem Feb 17 '17 at 19:34

As an alternative to making a selection, with a SearchCursor you have the option to use the where_clause parameter, which will limit the records returned.

See Building a Query Expression for more information.

| improve this answer | |

Instead of opening the SearchCursor on the whole shapefile you could open it on a layer that has that shapefile as its data source.

Any selections (spatial and/or attribute) on that layer will be respected by the cursor.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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