0

Working on ArcMap Desktop 10.8;

I have a point layer with a field called SITEROAD (string) from which I need to extract ONLY the first word before a space, and put it into a new field, STREETNAME.

Example:

Aberdeen way   needs to be calculated to Aberdeen 
ACACIA WAY     needs to be calculated to  Acacia
Aegean WY      needs to be calculated to  Aegean

I am trying:

def getFirst(inField):

  if inField is None:

   return None

  else:

   return inField.Split('')[0]

Field Calculator

2
  • 1
    Python is a case-sensitive language. The str.split() function has a lower-case name. Also, the else is unnecessary after the return None – Vince Jun 18 '20 at 1:50
  • 2
    When you ask a question like this, you need to include what happens when you run your code. If you get an error, you need to copy that error and include it in your question as text.\ – user2856 Jun 18 '20 at 1:57
4

Try the following function in the field calculator:

def splitText(f):
    split = f.split()
    if len(split) == 0:
        return ""
    return split[0].title()

enter image description here

The above function will check if you have empty street names, it will return empty string, otherwise it will return the first name in title case existing in the street name column.

Here is the output:

enter image description here

4
  • This worked beautifully. Thank you very much! – user165710 Jun 18 '20 at 3:47
  • 2
    @user165710 Please accept the answer if it solved the problem. – ahmadhanb Jun 18 '20 at 4:05
  • 1
    Note that this solution will convert 'East Aberdeen Way' to 'East', and if you slice off the last token, 'East Aberdeen Way, NW' becomes 'East Aberdeen Way,'. Street name slicing is more dark art than science. – Vince Jun 18 '20 at 13:48
  • Vince: I run into this issue; and now is my next problem. How do I deal with the streets that have two or 3 words, LIKE in ADMIRAL CALLAHAN that I need to become ADMIRAL CALLAHAN. I was thinking that I would skip to == and put 1 in there and then concatenate the words; something like def splitText(f): split = f.split() if len(split) == 1: return " " else: return split[0,1].title() But this does not work. – user165710 Jun 23 '20 at 0:06

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.