If you need to edit or delete rows from a table or feature class, you can use UpdateCursor
. As is the case with InsertCursor
, the contents of UpdateCursor
can be limited through the use of a where
clause.
The UpdateCursor()
function can be used to either update or delete rows in a table or feature class. The returned cursor places a lock on the data, which will automatically be released if used inside a Python with
statement. An UpdateCursor
object is returned from a call to this method.
The UpdateCursor
object places a lock on the data while it's being edited or deleted. If the cursor is used inside a Python with
statement, the lock will automatically be freed after the data has been processed. This hasn't always been the case. Previous versions of cursors were required to be manually released using the Python del
statement. Once an instance of UpdateCursor
has been obtained, you can then call the updateCursor()
method to update records in tables or feature classes and the deleteRow()
method can be used to delete a row.
In this recipe, you're going to write a script that updates each feature in the FireIncidents
feature class by assigning a value of poor
, fair
, good
, or excellent
to a new field that is more descriptive of the confidence values using an UpdateCursor
. Prior to updating the records, your script will add the new
field to the FireIncidents
feature class.
Follow these steps to create an UpdateCursor
object that will be used to edit rows in a feature class:
C:ArcpyBookCh8UpdateWildfires.py
.arcpy
module:import arcpy
arcpy.env.workspace = "C:/ArcpyBook/Ch8/WildfireData/WildlandFires.mdb"
try
block:try:
CONFID_RATING
to the FireIncidents
feature class. Make sure to indent inside the try
statement:arcpy.AddField_management("FireIncidents","CONFID_RATING", "TEXT","10") print("CONFID_RATING field added to FireIncidents")
UpdateCursor
inside a with
block:with arcpy.da.UpdateCursor("FireIncidents", ("CONFIDENCEVALUE","CONFID_RATING")) as cursor:
with
block:cntr = 1
FireIncidents
fire class. Update the CONFID_RATING
field according to the following guidelines: POOR
FAIR
GOOD
EXCELLENT
This can be translated in the following block of code:
for row in cursor: # update the confid_rating field if row[0] <= 40: row[1] = 'POOR' elif row[0] > 40 and row[0] <= 60: row[1] = 'FAIR' elif row[0] > 60 and row[0] <= 85: row[1] = 'GOOD' else: row[1] = 'EXCELLENT' cursor.updateRow(row) print("Record number " + str(cntr) + " updated") cntr = cntr + 1
except
block to print any errors that may occur:except Exception as e: print(e.message)
import arcpy arcpy.env.workspace = "C:/ArcpyBook/Ch8/WildfireData/WildlandFires.mdb" try: #create a new field to hold the values arcpy.AddField_management("FireIncidents", "CONFID_RATING","TEXT","10") print("CONFID_RATING field added to FireIncidents") with arcpy.da.UpdateCursor("FireIncidents",("CONFIDENCEVALUE", "CONFID_RATING")) as cursor: cntr = 1 for row in cursor: # update the confid_rating field if row[0] <= 40: row[1] = 'POOR' elif row[0] > 40 and row[0] <= 60: row[1] = 'FAIR' elif row[0] > 60 and row[0] <= 85: row[1] = 'GOOD' else: row[1] = 'EXCELLENT' cursor.updateRow(row) print("Record number " + str(cntr) + " updated") cntr = cntr + 1 except Exception as e: print(e.message)
C:ArcpyBookcodeCh8UpdateWildfires.py
solution file.Record number 406 updated Record number 407 updated Record number 408 updated Record number 409 updated Record number 410 updated
FireIncidents
feature class. Open the attribute table and you should see that a new CONFID_RATING
field has been added and populated by UpdateCursor
:When you insert, update, or delete data in cursors, the changes are permanent and can't be undone if you're working outside an edit session. However, with the new edit session functionality provided by ArcGIS 10.1, you can now make these changes inside an edit session to avoid these problems. We'll cover edit sessions soon.
In this case, we've used UpdateCursor
to update each of the features in a feature class. We first used the Add Field
tool to add a new field called CONFID_RATING
, which will hold new values that we assign based on values found in another field. The groups are poor, fair, good, and excellent and are based on numeric values found in the CONFIDENCEVALUE
field. We then created a new instance of UpdateCursor
based on the FireIncidents
feature class, and returned the two fields mentioned previously. The script then loops through each of the features and assigns a value of poor, fair, good, or excellent to the CONFID_RATING
field (row[1]
), based on the numeric value found in CONFIDENCEVALUE
. A Python if
/elif
/else
structure is used to control the flow of the script based on the numeric value. The value for CONFID_RATING
is then committed to the feature class by passing the row variable into the updateRow
() method.