Select to view content in your preferred language

Split Two Part Street Names into a single field?

760
3
Jump to solution
10-24-2022 07:57 PM
TimHayes3
Frequent Contributor

I am new to python.

I have encountered this situation, with about 1,000 street names that look like this in the single streetname field of my feature class:

 

San Marino Dr

El Cajon St

San Andreas St

 

How do I split "San Marino Dr" into 2 separate fields, one field will contain "San Marino" and the other field will contain "Dr"? Would it be easier to do a field calculate on each of the two fields I plan to split the streetname into? 

 

Doing a field calculate on a new empty field named Street1, here is where I get stuck:

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

This will give me "San", but how do I get "Marino"?

My second related question, where do I find a good Pro Resource for showing how to use ".split()" in Python?

 

0 Kudos
1 Solution

Accepted Solutions
JohannesLindner
MVP Frequent Contributor

You probably won't find ESRI documentation for split(), as that's a basic Pythonj function, not something ESRI developed.

split() takes a string and splits it at the given character. Optionally, you can tell it how many splits to do. It outputs a list.

"this is a string".split(" ")
# ['this', 'is', 'a', 'string']

"this is a string".split(" ", 1)
# ['this', 'is a string']

 

The official documentation is here (scroll to str.split): 4. Built-in Types — Python 3.3.7 documentation

An easier documentation is here: Python String split() Method (w3schools.com)

 

A list is a collection of elements that you can address by using their position in the collection (their index). Examples can be found here: Python Indexing and Slicing for Lists and other Sequential Types | Railsware Blog

 

 

As for your problem: Yes, do field calculations for the two fields.

For the field that contains the road type:

!Street_Name!.split(" ")[-1]

This will get the last element of the split name.

 

For the field that contains the name:

" ".join(!Street_Name!.split(" ")[:-1])

This gets all elements of the split name, up to (excluding) the last. Then it joins those elements together into a single string (the opposite of split()).

 

"San Marino Dr".split(" ")[-1]
# 'Dr'

" ".join("San Marino Dr".split(" ")[:-1])
# 'San Marino'

Have a great day!
Johannes

View solution in original post

3 Replies
JohannesLindner
MVP Frequent Contributor

You probably won't find ESRI documentation for split(), as that's a basic Pythonj function, not something ESRI developed.

split() takes a string and splits it at the given character. Optionally, you can tell it how many splits to do. It outputs a list.

"this is a string".split(" ")
# ['this', 'is', 'a', 'string']

"this is a string".split(" ", 1)
# ['this', 'is a string']

 

The official documentation is here (scroll to str.split): 4. Built-in Types — Python 3.3.7 documentation

An easier documentation is here: Python String split() Method (w3schools.com)

 

A list is a collection of elements that you can address by using their position in the collection (their index). Examples can be found here: Python Indexing and Slicing for Lists and other Sequential Types | Railsware Blog

 

 

As for your problem: Yes, do field calculations for the two fields.

For the field that contains the road type:

!Street_Name!.split(" ")[-1]

This will get the last element of the split name.

 

For the field that contains the name:

" ".join(!Street_Name!.split(" ")[:-1])

This gets all elements of the split name, up to (excluding) the last. Then it joins those elements together into a single string (the opposite of split()).

 

"San Marino Dr".split(" ")[-1]
# 'Dr'

" ".join("San Marino Dr".split(" ")[:-1])
# 'San Marino'

Have a great day!
Johannes
BlakeTerhune
MVP Regular Contributor

This is a perfect answer. However, I want to add something that I found in our organization while doing something similar. We have some streets that don't have a street suffix. You should make sure that scenario does not exist or code to handle it.

TimHayes3
Frequent Contributor

Thank you, Johannes! this was super helpful. Exactly what I was looking for. 

0 Kudos