I’m being tasked for developing a script tool that can find ultimately needs to be able to find any string and replace it with another string in an attribute table. Below is what I have so far, and it seems to be on its way to working except but in a very strict and limited way.
Some ways I want/need to spruce it up include:
- Being able to skip over OBJECTID and SHAPE fields so they cannot be changed…I figure this could be achieved by having a parameter that allows several entries, but I’m not sure how that would work. I found some sort of “.split(;)” thing to attach to the targetFields parameter earlier today but it didn’t seem to cooperate.
- Being able to find and replace partial strings
- I would also like the script to be able to handle integers and floats, AND I would like radio buttons in the final tool that would let me select between strings, ints, and floats.
import arcpy
FC = arcpy.GetParameterAsText(0)
oldText = arcpy.GetParameterAsText(1)
replaceText = arcpy.GetParameterAsText(2)
targetFields = arcpy.GetParameterAsText(3)
queryString = '"' + targetFields + '" = ' + "'" + oldText + "'"
with arcpy.da.UpdateCursor(FC, (targetFields,), queryString) as cursor:
for row in cursor:
row[0] = replaceText
cursor.updateRow(row)