I am trying to use an UpdateCursor to update a feature class by referencing a Search Cursor.
Here is a sample of my code. Note that I consulted the following ESRI document https://support.esri.com/en/technical-article/000019111 to troubleshoot. The feature class is registered as versioned and I created the cursor as an object as opposed to a loop. Edit.startEditing is set to False ,False as suggested in the ESRI document.
import arcpy
arcpy.env.workspace = r'Database Connections\THIS IS A VERSION'
# Create variables
FC1 = r'Database Connections\...
FC2 = r'Database Connections\...
fields = ['A','B','C'] expression = "B = 6"
#Search Cursor for item 1
scursor = arcpy.da.SearchCursor(FC2,fields,where_clause=expression)
# Process: Start Edit
edit = arcpy.da.Editor(arcpy.env.workspace)
edit.startEditing(False,False)
edit.startOperation()
# UpdateCursor for item 2
cursor = arcpy.da.UpdateCursor(FC1, fields)
for row in cursor:
row[1] == scursor[row[1]]
cursor.updateRow(row)
print(u'{0},{1},{2}'.format(row[0],row[1],row[2]))
del row
del cursor
edit.stopOperation()
edit.stopEditing(True)
Solved! Go to Solution.
I modified the script as seen in my provided script to suit my needs. There is only one thing I would like to change. The actual field that I want to change an attribute must match the exact naming for FC1 and FC2. I want to set it up so that the each field NAME can be different.
import arcpy
arcpy.env.workspace = (r'Database Connections...')
# Create variables
FC1 = r'Database Connections\...
FC1_fields = ['INVENTORYID','SAMPLE']
#expression = "INVENTORYID = 1"
FC2 = r'Database Connections\...'
FC2_fields = ['INVENTORYID','ANOTHERSAMPLE']
# Create empty dictionary to hold INVENTORYID values from FC2
# Create Search Cursor
# Populate dictionary, then search cursor closes automatically
fc2_lookup = {}
with arcpy.da.SearchCursor(FC2, FC2_fields) as cursor:
for row in cursor:
# create variables to hold row values
# not necessary, but helps with readability
a = row[0] # INVENTORYID
b = row[1]
# Used to look up existing INVENTORYID values in list
fc2_lookup = {"INVENTORYID": a, "B":b}
# Process: Start Edit
edit = arcpy.da.Editor(arcpy.env.workspace)
edit.startEditing(False,True)
edit.startOperation()
# use with again for updatecursor
with arcpy.da.UpdateCursor(FC1, FC1_fields) as cursor:
for row in cursor:
# FC1 fields to variables
a = row[0] # INVENTORYID
b = row[1]
print("initial row: INVENTORYID: {}, B: {}".format(a, b))
# check A column to see if INVENTORYID is in lookup (FC2)
if a in fc2_lookup:
# update a field in row using values from dictionary
#row[0] = fc2_lookup["A"]
row[1] = fc2_lookup["B"] # is INVENTORYID which look to see if match and ["B"] is the Dictionary Key value for what will be up...
edit.stopOperation()
edit.stopEditing(True)
print "done"