Use the update cursor to modify existing records in a dataset. Here are the general steps for using the update cursor:
- Create the update cursor by calling arcpy.da.UpdateCursor(). You can optionally pass in an SQL expression as an argument to this method. This is a good way to narrow down the rows you want to edit if you are not interested in modifying every row in the table.
- Use a for loop to iterate through the rows and for each row ...
- Modify the field values in the row that need updating (see below).
- Call UpdateCursor.updateRow() to finalize the edit.
Modifying field values
When you create an UpdateCursor and iterate through the rows using a variable called row, you can modify the field values by making assignments using the syntax row[<index of the field you want to change>] = <the new value>. For example:
row = "Trisha Stevens"
It is important to note that the index occurring in the [...] to determine which field will be changed is given with respect to the tuple of fields provided when the UpdateCursor is created. For instance, if we create the cursor using the following command
with arcpy.da.UpdateCursor(featureClass, ("Company name", "Owner")) as cursor:
row would refer to the field called "Company name" and row refers to the field that has the name "Owner".
The script below performs a "search and replace" operation on an attribute table. For example, suppose you have a dataset representing local businesses, including banks. One of the banks was recently bought out by another bank. You need to find every instance of the old bank name and replace it with the new name. This script could perform that task automatically.
#Simple search and replace script import arcpy # Retrieve input parameters: the feature class, the field affected by # the search and replace, the search term, and the replace term. fc = arcpy.GetParameterAsText(0) affectedField = arcpy.GetParameterAsText(1) oldValue = arcpy.GetParameterAsText(2) newValue = arcpy.GetParameterAsText(3) # Create the SQL expression for the update cursor. Here this is # done on a separate line for readability. queryString = '"' + affectedField + '" = ' + "'" + oldValue + "'" # Create the update cursor and update each row returned by the SQL expression with arcpy.da.UpdateCursor(fc, (affectedField,), queryString) as cursor: for row in cursor: row = newValue cursor.updateRow(row) del row, cursor
Notice that this script is relatively flexible because it gets all the parameters as text. However, this script can only be run on string variables because of the way the query string is set up. Notice that the old value is put in quotes, like this: "'" + oldValue + "'". Handling other types of variables, such as integers, would have made the example longer.
Again, it is critical to understand the tuple of affected fields that you pass in when you create the update cursor. In this example, there is only one affected field (which we named affectedField), so its index position is 0 in the tuple. Therefore, you set that field value using row = newValue. Cursor cleanup is not required at the end of the script because this is accomplished through the "with" statement.
The last line with updateRow(...) is needed to make sure that the modified row is actually written back to the attribute table. Please note that the variable row needs to be passed as a parameter to updateRow(...).
Dataset locking again
As we mentioned, ArcGIS sometimes places locks on datasets to avoid the possibility of editing conflicts between two users. If you think for any reason that a lock from your script is affecting your dataset (by preventing you from viewing it, making it look like all rows have been deleted, and so on), you must close PythonWin to remove the lock. If you think that ArcGIS has a lock on your data, check to see if ArcMap or ArcCatalog are using the data in any way. This could possibly occur through having an open edit session on the data, having the data open in the Preview tab in ArcCatalog, or having the layer in the table of contents in an open map document (MXD).
As we stated, cursor cleanup should happen through the creation of the cursor inside a "with" statement, but adding lines to delete the row and cursor objects will make extra sure locks are released.
For the Esri explanation of how locking works, you can review the section "Cursors and locking" in the topic Accessing data using cursors in the ArcGIS Desktop Help.