3

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.

1
  • 1
    "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 your if statement in any Python interpreter.
    – Vince
    Commented Oct 30, 2019 at 14:11

2 Answers 2

2

The syntax is a bit different:

with arcpy.da.UpdateCursor(fc, ['Colour', 'Description']) as cursor:
    for row in cursor:
      if 'red' in str(row[1]):
           row[0] = 'red' 
      elif 'orange' in str(row[1]):
           row[0] = 'orange' 
      else:
           row[0] = 'no_color'
      cursor.updateRow(row)

Also it might be a good idea to convert the row to lower_case:

 str(row[1]).lower()

Or it will not match red and Red and so on

As per comment, if you want more foolproof design, you can use regex match. It really depends on the complexity of the problem.

import re
if re.search("^patternhere$", row[1]:
   row[0] = 'Red'

For more regex search: https://stackoverflow.com/questions/7879600/using-anchors-in-python-regex-to-get-exact-match

2
  • 2
    This would falsely encode "The ball was rediculously orange" as "red"
    – Vince
    Commented Oct 30, 2019 at 14:14
  • Sure, the you would need to use regex match, it depends on the complexity. I would use if re.search(pattern, str(row[1]) Commented Oct 30, 2019 at 14:18
1
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 'red' in row[0]:
            row[0] = 'Red'
        elif 'blue' in row[0]:
            row[0] = 'Blue'
        cursor.updateRow(row)
print("Finished")

You can wildcard using regex or fnmatch: https://stackoverflow.com/questions/11427138/python-wildcard-search-in-string

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.