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

PolyGeo
65.5k29 gold badges115 silver badges350 bronze badges
asked Dec 13, 2018 at 22:46
3
  • 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? Commented Dec 13, 2018 at 22:51
  • 1
    It seems to be shapefile table. None is not supported. Commented Dec 13, 2018 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. Commented Dec 13, 2018 at 22:55

1 Answer 1

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'

answered Dec 13, 2018 at 23:00

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.