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?
1 Answer 1
If you plan to do this often you might be better off doing this as a function so you can reuse it.
- Open the Field Calculator
- Click on the Show Codeblock check box
- Enter the code block included below
- 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
Explore related questions
See similar questions with these tags.
str(!field!).replace('Aa', 'Rr').replace('Bb', 'Rr').replace('Cc', 'Rr')
?