0

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.

enter image description here

  • 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 Dec 13 '18 at 22:51
  • 1
    It seems to be shapefile table. None is not supported. – FelixIP Dec 13 '18 at 22:53
  • 1
    Please always provide code as formatted text rather than only in a picture. That way potential answerers can copy/paste it to test. – PolyGeo Dec 13 '18 at 22:55
2

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'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.