More

A script to populate a numeric field in Arcgis based on some content of a string field?

A script to populate a numeric field in Arcgis based on some content of a string field?


I have field in an attribute table populated with expressions like "6S2Olch1E", "10S1B", "9B3S2Olch" and so on. I need to make a numeric filed populated with only numbers associeted with letter S from those experessions (f.e. 6 if expression is "6S2Olch1E" or 10 if it's "10S1B"). Is there a way to do this using a script in Field Calculator? I'm not a programmer.


Standalone code

def f(s): idx = s.upper().index('S') s = s[:idx] for i in reversed(range(len(s))): if s[i].isalpha(): s = s[i+1:] break if s.isdigit(): return int(s) else: return 0

Testing

print f("S2Olch1E") #0 print f("6S2Olch1E") #6 print f("5B66S2Olch1E") #66 print f("54F5456B666S2Olch1E") #666

In your expression (Field Calculator), just use:

f(!fieldName!)

Watch the video: Automating Text Based Field Calculator Operations using Python Scripts