Cursor (arcpy)

摘要

游标是一种数据访问对象,可用于在表中迭代一组行或者向表中插入新行。游标有三种形式:搜索插入更新。游标通常用于读取和更新属性。

方法概述

方法说明
deleteRow (row)

Deletes a row in the database. The row corresponding to the current position of the cursor will be deleted.

insertRow (row)

Inserts a new row into the database.

newRow ()

Creates an empty row object.

next ()

Returns the next object at the current index.

reset ()

Sets the current enumeration index (used by the next method) back to the first element.

updateRow (row)

The updateRow method can be used to update the row at the current position of an update cursor.

方法

deleteRow (row)
参数说明数据类型
row

The row to be deleted.

Row
insertRow (row)
参数说明数据类型
row

The row to be inserted.

Row
newRow ()
返回值
数据类型说明
Row

A new empty row object.

next ()
返回值
数据类型说明
Object

The next object at the current index.

reset ()
updateRow (row)
参数说明数据类型
row

The row used to update the current position of the cursor.

Row

代码实例

游标示例 1

使用搜索游标查看行中的字段值。

import arcpy

# Set the workspace
arcpy.env.workspace = "c:/base/data.gdb"

# Create the search cursor
cursor = arcpy.SearchCursor("roads", '"TYPE" <> 4')

# Iterate through the rows in the cursor
for row in cursor:
    print("Name: {0},  CFCC code: {1}".format(row.NAME, row.CFCC))

del cursor, row
游标示例 2

使用更新游标更改行中的字段值。

import arcpy

# Set the workspace
arcpy.env.workspace = "c:/base/data.gdb"

# Create the update cursor
cursor = arcpy.UpdateCursor("roads")

# Update the road buffer distance field based on road type.
#   Road type is either 1,2,3,4  Distance is in meters.
for row in cursor:
    row.setValue("BUFFER_DIST", row.getValue("TYPE") * 100)
    cursor.updateRow(row)

# Delete cursor and row objects
del cursor, row
游标示例 3

使用插入游标在表中新建行。

import datetime
import arcpy

# Create insert cursor for table
cursor = arcpy.InsertCursor("c:/base/data.gdb/roads_maint")

# Create 25 new rows. Set default values on distance and CFCC code
for i in xrange(1000, 1025):
    row = cursor.newRow()
    row.setValue('rowid', i)
    row.setValue('distance', 100)
    row.setValue('CFCC', 'A10')
    row.setValue('LastInsp', datetime.datetime.now())
    cursor.insertRow(row)

# Delete cursor and row objects
del cursor, row

相关主题

5/10/2014