[GIS] Replacing multiples words using field calculator in attribute table python

arcgis-desktopfield-calculatorfields-attributesfindpython

I need to replace multiple words with one word in a field at one time in one script.

Example in my LettersField I need to replace all the "Aa" and all the "Bb" and all the "Cc" with "Rr".

I am aware of the replace fxn and I cannot use the find and replace tool – which would be MUCH easier but I have to create a python script.

How can I do this – what would the code block be and the 'LettersField =' be?

Best Answer

If you plan to do this often you might be better off doing this as a function so you can reuse it.

  1. Open the Field Calculator
  2. Click on the Show Codeblock check box
  3. Enter the code block included below
  4. You can click on the Save button to save the code and load it later on with the Load button

Here is a screen capture as an example. enter image description here

It's the same result in the end but it may be a bit more flexible.

Pre-Logic Script Code

def rep_field(in_fld, rep_value):
  targets = ['Aa','Bb','Cc','Dd']
  for targ in targets:
    in_fld = in_fld.replace(targ, rep_value)

  return in_fld
Related Question