Field Calculator Python String Manipulation - anthonyblackham/GIS-Wiki GitHub Wiki
Sometimes you'll want to compare two fields but the formats are just slightly different. You can use python to manipulate the strings so they both have the same format and will yield more accurate results when comparing two fields.
Find and Replace
There are a few ways to do a find and replace. You can click on the table options in the attribute table and select find and replace and use the gui there.
You can also use the .replace
function in the field calculator:
!FIELD!.replace("Find","Replace")
So if I had a field with _
and I want to replace them all with -
then it would be:
!FIELD!.replace("_","-")
Of course you can also use an if then statement in the field calculator in order to have a little more flexibility in the results.
Case Sensitivity
Sometimes programs are picky about case sensitivity, python makes it easy to convert the cases.
If I want to make all the letters lowercase:
!Field! .lower()
If I want to make all the letters uppercase:
!Field! .upper()