Thursday, May 19, 2011

Search Cursors - Using Expressions

In the last post, search cursor basics were discussed.  When creating search cursors, you can specify many properties, one way to limit the number of row object returned to the end user is the use SQL expressions.


import arcpy
sqlExpression = "\"STATE_NAME\" = 'California'"
fc = r"c:\temp\counties.shp"
rows = arcpy.SearchCursor("C:/Data/Counties.shp", sqlExpression)
# Do stuff with rows


Here only the California counties will be returned.

Enjoy

2 comments:

Jason said...

A better and cleaner way to construct this expression string is to use triple quotes...

"""STATE_NAME = 'California'"""

Andrew said...

Thanks for the suggestion.