Select to view content in your preferred language

Split values of a name filed

849
6
07-05-2022 07:01 AM
MariyanaKostov1
Occasional Contributor

I have a filed containing first, middle, and last name Ex:Emily Smith Brannen.

I want each name in a different field. In other word: the siring before the first space in one filed, the string between first and second space in another field, and the string before the last space in a third field.

39032725-49e9-4453-b860-5414d971f3df.png image.png

The script works If I do the calculation on one record but not on the whole filed.

Any help would be appreciated.

 

0 Kudos
6 Replies
DanPatterson
MVP Esteemed Contributor

strip any potential leading trailing spaces.  See the example output below... the last print line is correct,

for n in name:
    print(n.split(" "))
    print(n.split(" ")[1])
    print(n.strip().split(" ")[1])
    
['a', 'b', 'c']
b
b
['d', 'e', '']
e
e
['', 'f', 'g', 'h', '']
f
g

... sort of retired...
0 Kudos
ChrisWiebke
Regular Contributor

Are there any null values in your [Name] column?  'NoneType' objects do not have a 'split' attribute.

0 Kudos
MariyanaKostov1
Occasional Contributor

How do I check for null values? Our secretary typed in the names so I don't know if she typed any 'crazy' values:-)

0 Kudos
by Anonymous User
Not applicable

That could be a lot things causing that error.  Since it looks like the names do not contain all three (first, middle, last), you need to account for those like Clint. You can do that with using a function.  Make sure python is checked, and Show Codeblock is checked. If there are only two, like Clint, youll need to skip the middle name because Shugart is probably their last name.  Indenting counts here, and can be a pain so make sure you use 4 spaces per indent if the code doesn't copy paste correctly.

fieldcalc_First.png

 

def namesplit(name):
    splt = name.split(' ')
    if splt[0]:
        return splt[0]
    else:
        return None

# and in the FirstName block:
namesplit(!Name!)

 

fieldcalc_Middle.png

 

def namesplit(st_name):
    splt = st_name.split(' ')
    if len(splt) > 2:
        if splt[1]:
            return splt[1]
    else:
       return None

 

and last name code:

 

def namesplit(st_name):
    splt = st_name.split(' ')
    if len(splt) == 2:
        if splt[1]:
            return splt[1]
    elif  len(splt) > 2:
        if splt[2]:
            return splt[2:]
    else:
        return None

 

added return splt[2:] to capture the Sr, Jr, etc.

0 Kudos
MariyanaKostov1
Occasional Contributor

I was able to get the first and last string. Not sure how to get the middle

MariyanaKostov1_1-1657031671499.png

 

 

0 Kudos
JohannesLindner
MVP Frequent Contributor
def split_name(name):
    """splits a name (str) into 3 parts (first, middle, last)"""
    try:
        name_parts = name.strip().split(" ")
    except AttributeError:
        # handle non-strings
        return [None, None, None]
    first = name_parts[0]
    last = name_parts[-1]
    middle = " ".join(name_parts[1:-1])
    if middle == "":
        middle = None
    return [first, middle, last]


split_name('David Smith')
#['David', None, 'Smith']

split_name('David "The cooler David" Johnson')
#['David', '"The cooler David"', 'Johnson']

split_name("Clinton B Brennan")
#['Clinton', 'B', 'Brennan']

 

Use that function as code block and these expressions to calculate the fields:

FirstName = split_name(!Name!)[0]
MiddleName = split_name(!Name!)[1]
LastName = split_name(!Name!)[2]

 


Have a great day!
Johannes