Arcpy—updateCursor更新和删除字段数据
1 # -*- coding: utf-8 -*- 2 import arcpy 3 4 arcpy.env.workspace = "F:\ArcpyBook\Ch8\WildfireData\WildlandFires.mdb" #S设置工作空间,这里是shp所在的位置 5 6 f = open(r"F:\ArcpyBook\Ch8\WildfireData\NorthAmericaWildfires_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:\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)
--------------------成功,肯定是需要一点一滴积累的--------------------