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?
Solved! Go to Solution.
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'
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'
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.
Thank you, Johannes! this was super helpful. Exactly what I was looking for.