I have a file geodatabase feature class (fc). I want to update Field 1 (Colour) with text where Field 2 (Description) contains a certain word. ie. Field 2 contains the string 'the ball is red and small' so...
if Field 2 contains %red% then update Field 1 with the value 'Red'
In ArcPy I've tried this to no avail:
import arcpy
arcpy.env.overwriteOutput = True
fc = r"D:\Pathtodata\database.gdb\featureclass"
with arcpy.da.UpdateCursor(fc, ['Colour', 'Description']) as cursor:
for row in cursor:
if row[1] == '%red%':
row[0] = 'Red'
elif row[1] == '%blue%':
row[0] = 'Blue'
cursor.updateRow(row)
print("Finished")
When I run this, no updates are made. It cannot seem to use wildcards. It only seems to work with exact matches ie:
with arcpy.da.UpdateCursor(fc, ['Colour', 'Description']) as cursor:
for row in cursor:
if row[1] == 'the ball is red and small':
row[0] = 'Red'
....
etc. etc.
How can I make this work?
I'm working in a standalone Python script, in PyCharm, not in ArcGIS.
"red" in row[1]
would work better than an equivalence test (though this will also match many other words, and won't match "Red ball"). It doesn't matter which Python environment you use, just that you use a valid Python comparison. You can prototype yourif
statement in any Python interpreter.