Label Expression

3780
9
Jump to solution
07-31-2017 01:27 PM
EmmanuelJohn1
Emerging Contributor

Hello,

      does anyone know of an expression that can help me make a label of everything before the first space within a field?? 

Example:

            Home numbers of an address field.

0 Kudos
1 Solution

Accepted Solutions
IanMurray
Honored Contributor

Using Python Parser.

def FindLabel ( [fieldname]):
  return [fieldname].split(" ")[0]‍‍

I'm assuming that the field in question is a text field.  This splits a text string by a space character(.split(" ") and returns the first portion of that string(actually splits the whole string into a list of strings and returns the first item of that list hence the [0])

View solution in original post

9 Replies
IanMurray
Honored Contributor

Using Python Parser.

def FindLabel ( [fieldname]):
  return [fieldname].split(" ")[0]‍‍

I'm assuming that the field in question is a text field.  This splits a text string by a space character(.split(" ") and returns the first portion of that string(actually splits the whole string into a list of strings and returns the first item of that list hence the [0])

EmmanuelJohn1
Emerging Contributor

0 Kudos
EmmanuelJohn1
Emerging Contributor

Thank You Ian for introducing Split concept, I was able to use this in the field calculator with python parser

!fieldname!.split(" ")[0]

0 Kudos
AdrianWelsh
MVP Honored Contributor

Emmanuel,

Please mark Ian's response as Correct to close out this thread. 

0 Kudos
kathyhardinwalker
Regular Contributor

This is not working for us in ArcGIS Pro. We are also trying to return everything before the first space (or drop everything after the first space). I attached a picture of what the Invalid Expression response was. Thank you in advance for saving the day 

Kathy

0 Kudos
JoeBorgione
MVP Emeritus

kathy walker‌:  You may need to put exclamations around the [ADDRESS] in line 1:

That should just about do it....
0 Kudos
kathyhardinwalker
Regular Contributor

Thank you Joe. We finally got something that works:

def FindLabel ( [Location] 😞
    if [Location] == None: 
         return "No Address"
    else:
         x = [Location]
         x1 = x.split(" ")
         return x1[0]

0 Kudos
JoeBorgione
MVP Emeritus

kathy walker -  I just gave the original expression a go and it worked for me.  It appears that when using the python parsing tool ArcGIS Pro understands that exclamations are implied. (Like the use of quotes as well...) I thought your problem might have been None values, but my data has them too.  Not sure what's up there....

That should just about do it....
0 Kudos
kathyhardinwalker
Regular Contributor

One problem we discovered, the field we were using was indexed, it had an asterisk (Address*) in the header. As soon as we switched to a field that was not indexed we started getting somewhere.

0 Kudos