Arcpy—updateCursor更新和删除字段数据

Posted gouq

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Arcpy—updateCursor更新和删除字段数据相关的知识,希望对你有一定的参考价值。

 1 # -*- coding: utf-8 -*-
 2 import arcpy
 3 
 4 arcpy.env.workspace = "F:ArcpyBookCh8WildfireDataWildlandFires.mdb"  #S设置工作空间,这里是shp所在的位置
 5 
 6 f = open(r"F:ArcpyBookCh8WildfireDataNorthAmericaWildfires_2007275.txt",r)
 7 lsFires = f.readlines()
 8 try:
 9     arcpy.AddField_management("FireIncidents","CONFID_RATING","TEXT","10")
10     with arcpy.da.UpdateCursor(FireIncidents,(CONFIDENCEVALUE,CONFID_RATING)) as cursor:
11         cntr = 1
12         for row in cursor:
13             if row[0] < 40:
14                 row[1] = POOR
15             elif row[0] > 40 and row[0] < 60:
16                 row[1] = FIRE
17             elif row[0] > 60 and row[0] < 85:
18                 row[1] = GOOD
19             else:
20                 row[1] = EXCELLENT
21             cursor.updateRow(row)    #有点类似于sql数据库的commit()
22             print("Recode number "+str(cntr)+"update")
23             cntr += 1
24 except Exception as e:
25     print(e)

 

删除行

# -*- coding: utf-8 -*-
import arcpy

arcpy.env.workspace = "F:ArcpyBookCh8WildfireDataWildlandFires.mdb"  #S设置工作空间,这里是shp所在的位置

f = open(r"F:ArcpyBookCh8WildfireDataNorthAmericaWildfires_2007275.txt",r)
lsFires = f.readlines()
try:
    arcpy.AddField_management("FireIncidents","CONFID_RATING","TEXT","10")
    with arcpy.da.UpdateCursor(FireIncidents,[CONFID_RATING],[CONFID_RATING]=‘POOR‘) as cursor:
        #这里第三个参数‘[CONFID_RATING]=‘POOR‘‘是使用了where字句限制了返回内容,其实就是一个sql查询语句
        #因为是在个人地理数据库中所以是[CONFID_RATING]而不是“CONFID_RATING”,
        #在shapfile文件、文件地理数据库和ArcSDE地理数据库中是使用双引号将字段名给引起来,但是在个人地理数据库中
        #是使用[]将字段名给括起来,这点需要注意。诸如此类的用法还有通配符,详见《基于ArcGIS的python编程秘籍》p152-p153
        cntr = 1
        for row in cursor:
            cursor.deleteRow()
            print("Recode number "+str(cntr)+"deleted")
            cntr += 1
except Exception as e:
    print(e)

 

以上是关于Arcpy—updateCursor更新和删除字段数据的主要内容,如果未能解决你的问题,请参考以下文章

[ArcPy Tips-1]根据指定字段的大小顺序,在另一字段中存储其序号

python使用游标访问数据

arcpy使用要点

ArcGIS 应用Python中arcpy模块

使用Arcpy批量修改矢量数据的字段值

基于arcpy实现工作中需要实现功能的经验和代码总结: