ArcGIS Desktop

  • ArcGIS Pro
  • ArcMap

  • My Profile
  • Help
  • Sign Out
ArcGIS Desktop

ArcGIS Online

The mapping platform for your organization

ArcGIS Desktop

A complete professional GIS

ArcGIS Enterprise

GIS in your enterprise

ArcGIS Developers

Tools to build location-aware apps

ArcGIS Solutions

Free template maps and apps for your industry

ArcGIS Marketplace

Get apps and data for your organization

  • Documentation
  • Support
Esri
  • Sign In
user
  • My Profile
  • Sign Out

ArcMap

  • Home
  • Get Started
  • Map
  • Analyze
  • Manage Data
  • Tools
  • Extensions

SearchCursor

  • Summary
  • Discussion
  • Syntax
  • Code sample

Summary

The SearchCursor function establishes a read-only cursor on a feature class or table. SearchCursor can be used to iterate through Row objects and extract field values. The search can optionally be limited by a where clause or by field and optionally sorted.

Discussion

Legacy:

This function was superceded by arcpy.da.SearchCursor at ArcGIS 10.1. For faster performance, use arcpy.da.SearchCursor.

Search cursors can be iterated with a for loop or in a while loop using the cursor's next method to return the next row. When using the next method on a cursor to retrieve all rows in a table containing N rows, the script must make N calls to next. A call to next after the last row in the result set has been retrieved returns None, which is a Python data type that acts here as a placeholder.

Using SearchCursor with a for loop.

import arcpy

fc = "c:/data/base.gdb/roads"
field = "StreetName"
cursor = arcpy.SearchCursor(fc)
for row in cursor:
    print(row.getValue(field))

Using SearchCursor with a while loop.

import arcpy

fc = "c:/data/base.gdb/roads"
field = "StreetName"
cursor = arcpy.SearchCursor(fc)
row = cursor.next()
while row:
    print(row.getValue(field))
    row = cursor.next()

Syntax

SearchCursor (dataset, {where_clause}, {spatial_reference}, {fields}, {sort_fields})
ParameterExplanationData Type
dataset

The feature class, shapefile, or table containing the rows to be searched.

String
where_clause

An optional expression that limits the rows returned in the cursor. For more information on where clauses and SQL statements, see Building a query expression.

String
spatial_reference

When specified, features will be projected on the fly using the spatial_reference provided.

SpatialReference
fields

A semicolon-delimited string of fields to be included in the cursor. By default, all fields are included.

String
sort_fields

Fields used to sort the rows in the cursor. Ascending and descending order for each field is denoted by A for ascending and D descending, using the form "field1 A;field2 D".

String

Return Value

Data TypeExplanation
Cursor

A Cursor object that can hand out Row objects.

Code sample

SearchCursor example

List field contents for Counties.shp. Cursor is sorted by state name and population.

import arcpy

# Open a searchcursor
#  Input: C:/Data/Counties.shp
#  Fields: NAME; STATE_NAME; POP2000
#  Sort fields: STATE_NAME A; POP2000 D
rows = arcpy.SearchCursor("c:/data/counties.shp",
                          fields="NAME; STATE_NAME; POP2000",
                          sort_fields="STATE_NAME A; POP2000 D")

# Iterate through the rows in the cursor and print out the
# state name, county and population of each.
for row in rows:
    print("State: {0}, County: {1}, Population: {2}".format(
        row.getValue("STATE_NAME"),
        row.getValue("NAME"),
        row.getValue("POP2000")))

Related topics

  • Accessing data using cursors
  • UpdateCursor
  • InsertCursor

ArcGIS Desktop

  • Home
  • Documentation
  • Support

ArcGIS

  • ArcGIS Online
  • ArcGIS Desktop
  • ArcGIS Enterprise
  • ArcGIS
  • ArcGIS Developer
  • ArcGIS Solutions
  • ArcGIS Marketplace

About Esri

  • About Us
  • Careers
  • Esri Blog
  • User Conference
  • Developer Summit
Esri
Tell us what you think.
Copyright © 2021 Esri. | Privacy | Legal