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

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

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

f = open(r"F:\ArcpyBook\Ch8\WildfireData\NorthAmericaWildfires_2007275.txt",‘r‘)
lsFires = f.readlines()
try:
    arcpy.AddField_management("FireIncidents","CONFID_RATING","TEXT","10")
    with arcpy.da.UpdateCursor(‘FireIncidents‘,(‘CONFIDENCEVALUE‘,‘CONFID_RATING‘)) as cursor:
        cntr = 1
        for row in cursor:
            if row[0] < 40:
                row[1] = ‘POOR‘
            elif row[0] > 40 and row[0] < 60:
                row[1] = ‘FIRE‘
            elif row[0] > 60 and row[0] < 85:
                row[1] = ‘GOOD‘
            else:
                row[1] = ‘EXCELLENT‘
            cursor.updateRow(row)    #有点类似于sql数据库的commit()
            print("Recode number "+str(cntr)+"update")
            cntr += 1
except Exception as e:
    print(e)

删除行

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

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

f = open(r"F:\ArcpyBook\Ch8\WildfireData\NorthAmericaWildfires_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)

相关推荐