ArcGIS 幫助 10.2
摘要
SearchCursor 函數用於在要素類或表上建立只讀游標。SearchCursor 可用於遍歷行對象並提取字段值。可以使用 where 子句或字段限制搜索,並對結果排序。
討論
以迭代方式搜索游標的方式有兩種:for 循環或者 while 循環(通過游標的 next 方法返回下一行)。如果要使用游標的 next 方法來檢索行數為 N 的表中的所有行,腳本必須調用 next N 次。在檢索完結果集的最后一行后調用 next 將返回 None,它是一種 Python 數據類型,此處用作占位符。
通過 for 循環使用 SearchCursor。
語法
| 參數 | 說明 | 數據類型 |
|
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 About_building_an_SQL_expression. |
String |
|
spatial_reference
|
When specified, features will be projected on the fly using the spatial_reference provided. |
SpatialReference |
|
fields
|
The 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 and D. |
String |
代碼實例
列出 Counties.shp 的字段內容。游標按“州名稱”和“人口”排序。
1 import arcpy 2 3 # Open a searchcursor 4 # Input: C:/Data/Counties.shp 5 # Fields: NAME; STATE_NAME; POP2000 6 # Sort fields: STATE_NAME A; POP2000 D 7 rows = arcpy.SearchCursor("c:/data/counties.shp", 8 fields="NAME; STATE_NAME; POP2000", 9 sort_fields="STATE_NAME A; POP2000 D") 10 11 # Iterate through the rows in the cursor and print out the 12 # state name, county and population of each. 13 for row in rows: 14 print("State: {0}, County: {1}, Population: {2}".format( 15 row.getValue("STATE_NAME"), 16 row.getValue("NAME"), 17 row.getValue("POP2000")))
