2

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

2
  • It is easier not to add the prefix if it is already added
    – Bera
    Commented Jan 12, 2022 at 10:03
  • 2
    Select the records where Test does not start with City of, then run the field calculator
    – Berend
    Commented Jan 12, 2022 at 10:52

2 Answers 2

4
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

1
  • Thank you soo much :) Commented Jan 12, 2022 at 17:51
2

This will check if the field NAME already include the string 'City' and add the prefix only if not :

def Prefix (Name):
  if ('City' in Name):
       return Name
  else:
    return 'City of '+ Name

enter image description here

3
  • I wonder if places like Panama City and the like should be prefixed with City of. Your code will not do that, but maybe that's intended?
    – Berend
    Commented Jan 12, 2022 at 10:57
  • @Berend you are right my code will leave "Panama City" (and other name including City) as it is, I dont think it should be prefixed and become "City of Panama City" (I may be wrong on that). You could alter the code remplacing " if ('City' in Name) " by " if ('City of' in Name) " to prefix these case....
    – J.R
    Commented Jan 12, 2022 at 11:29
  • Thank you so much:) Commented Jan 12, 2022 at 17:51

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.