其他分享
首页 > 其他分享> > Arcpy学习笔记(二)之Arcpy函数-游标Cursor

Arcpy学习笔记(二)之Arcpy函数-游标Cursor

作者:互联网

一、InsertCursor:

向要素类、shapefile或表中插入行。InsertCursor返回一个分发行对象的枚举对象。

就是以一行为操作对象,并且可迭代。

说明:可以使用newRow方法从插入行的枚举对象获取新的行对象。每次调用光标上InsertRow都会在表中创建新行,该行的初始值设置为输入行中的值。

语法:

返回值:

 SearchCursor:用于在要素类或表上建立只读游标。SearchCursor可用于

遍历行对象并提取字段值。可以使用where子句或字段限制搜索,并对结果排序。

使用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()
#用while循环的时候,需要用.next方法返回下一行,如果要使用游标的 next 方法来检索行数为 N 的表中的所有行,则脚本必须调用 next N 次。在检索完结果集的最后一行后调用 next 将返回 None,它是一种 Python 数据类型,此处用作占位符。

语法:

返回值:

 SearchCursor示例:

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

 

 

标签:cursor,NAME,arcpy,游标,next,Cursor,SearchCursor,Arcpy,row
来源: https://blog.csdn.net/philosophy_jia/article/details/121427932