ArcMap – Removing Characters Using Field Calculator

arcgis-desktoparcmapfield-calculatormodelbuilderpython-parser

I am working with automating a workflow using ArcGIS ModelBuilder, and the final output returned an error in the resulting output.

Here the Test field is filled by adding a Prefix "City of" in front of the NAME field.

Now, if we observe the resulting Test field is having an error in the second row and is expected as the input NAME field already had the prefix added to it.

So, is there a way to remove this using the field calculator, I have tried using the replace and python slice notation, but it is applied on the entire column.

I can easily just manually edit the input or the output table but would like to know if there is an automated way, in case I get any such entry in the updated data received on regular basis.

enter image description here

Best Answer

def cleanit(name):
    if name.count('City of')>1:
            newname = [] #Create a list
            [newname.append(word) for word in name.split(' ') if word not in newname] #Split the string at each space, append word to list if it isnt already in it
            newname = ' '.join(newname) #Join back to a string
            return newname
    else:
        return name
    

enter image description here