This is Field Calculator in ArcMap. I am trying to split the word after the first space if the words starts with "San" like San Francsico, San Diego and etc. It return an error. Below is the script.
-
if 'SAN ' in inField.upper(): return inField.split(' ')[1] should work for you but if SAN isn't in the name you're returning Null which a shapefile (evident by the FID field) won't like. Also what would happen if the name had SAN as the 2nd element like North San Fransisco? Are you sure there are no cases of such in the Name field?Michael Stimson– Michael Stimson2018年12月13日 22:51:06 +00:00Commented Dec 13, 2018 at 22:51
-
1It seems to be shapefile table. None is not supported.FelixIP– FelixIP2018年12月13日 22:53:03 +00:00Commented Dec 13, 2018 at 22:53
-
1Please always provide code as formatted text rather than only in a picture. That way potential answerers can copy/paste it to test.PolyGeo– PolyGeo ♦2018年12月13日 22:55:30 +00:00Commented Dec 13, 2018 at 22:55
1 Answer 1
As mentioned in the comments there are a few problems in your code, in order to address some of them try this:
def getSec(inField):
if 'SAN' in inField.upper().split(' '):
return inField.split(' ')[inField.upper().split(' ').index('SAN')+1]
else:
return '' # better for a shapefile to return an empty string
Whenever string comparing always use upper() or lower() as python is case sensitive, then once you've determined the target string exists use index() to find it and get the next element (or even better use ' '.join( [...index():]) to get the next word to the end of the string):
' '.join(Name.split(' ')[Name.upper().split(' ').index('SAN')+1:])
Which will split strings like 'North San Fran'
to 'Fran'
, 'San Fran East'
to 'Fran East'
Explore related questions
See similar questions with these tags.