ArcGIS Desktop

  • ArcGIS Pro
  • ArcMap

  • My Profile
  • 帮助
  • Sign Out
ArcGIS Desktop

ArcGIS Online

专为贵组织打造的制图平台

ArcGIS Desktop

全面的专业性 GIS

ArcGIS Enterprise

面向企业的 GIS

ArcGIS for Developers

用于构建位置感知应用程序的工具

ArcGIS Solutions

适用于行业的免费模板地图和应用程序

ArcGIS Marketplace

获取适用于组织的应用程序和数据

  • 文档
  • 支持
Esri
  • 登录
user
  • 我的个人资料
  • 登出

ArcMap

  • 主页
  • 入门
  • 地图
  • 分析
  • 管理数据
  • 工具
  • 扩展模块

SearchCursor

  • 描述
  • 说明
  • 语法
  • 代码示例

描述

SearchCursor 函数用于在要素类或表上建立只读游标。SearchCursor 可用于遍历 Row 对象并提取字段值。可以使用 where 子句或字段限制搜索,并对结果排序。

说明

旧版本:

在 ArcGIS 10.1 中引入了 arcpy.da 游标(arcpy.da.SearchCursor、arcpy.da.UpdateCursor 和 arcpy.da.InsertCursor),与先前已存在的游标功能(arcpy.SearchCursor、arcpy.UpdateCursor 和 arcpy.InsertCursor)组相比性能要快得多。仍然会提供原始游标,不过仅仅是为了能够继续向后兼容。

可用于迭代搜索游标的方式有两种:for 循环或者 while 循环(通过游标的 next 方法返回下一行)。如果要使用游标的 next 方法来检索行数为 N 的表中的所有行,则脚本必须调用 next N 次。在检索完结果集的最后一行后调用 next 将返回 None,它是一种 Python 数据类型,此处用作占位符。

通过 for 循环使用 SearchCursor。

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

通过 while 循环使用 SearchCursor。

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()

语法

SearchCursor (dataset, {where_clause}, {spatial_reference}, {fields}, {sort_fields})
参数说明数据类型
dataset

包含要搜索行的要素类、shapefile 或表。

String
where_clause

用于限制在游标中返回的行的可选表达式。有关 where 子句和 SQL 语句的详细信息,请参阅 关于构建 SQL 表达式。

String
spatial_reference

指定后,要素将使用提供的 spatial_reference 进行动态投影。

SpatialReference
fields

游标中包含以分号分隔的字符串字段。默认情况下,包含所有字段。

String
sort_fields

用于在游标中对行进行排序的字段。每个字段的升序和降序排列表示为 "field1 A;field2 B" 形式,A 表示升序,D 表示降序。

String

返回值

数据类型说明
Cursor

可分发 Row 对象的 Cursor 对象。

代码示例

SearchCursor 示例

列出 Counties.shp 的字段内容。游标按州名称和人口进行排序。

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")))

相关主题

  • 使用游标访问数据
  • UpdateCursor
  • InsertCursor

ArcGIS Desktop

  • 主页
  • 文档
  • 支持

ArcGIS 平台

  • ArcGIS Online
  • ArcGIS Desktop
  • ArcGIS Enterprise
  • ArcGIS for Developers
  • ArcGIS Solutions
  • ArcGIS Marketplace

关于 Esri

  • 关于我们
  • 招贤纳士
  • Esri 博客
  • 用户大会
  • 开发者峰会
Esri
分享您的想法。
Copyright © 2019 Esri. | 隐私政策 | 法律声明