I am using Python in the Field Calculator to split an address field and I cannot find the string function I need for my function. Basically, what I want to do is create a function to say if 'CIR' is in the string name return everything before 'CIR', but not 'CIR'. I am just trying to get the street name, not the street type in the field.
Here is what I have so far:
def street_name (st_name):
if ' CIR' in st_name:
return
I cannot find a string function to return everything to the left of 'CIR'. Thank in advance everyone.
Solved! Go to Solution.
Hi Matthew,
Try the following:
def street_name (st_name):
if ' CIR' in st_name:
name = st_name.split(' CIR')[0]
return name
Hi Matthew,
Try the following:
def street_name (st_name):
if ' CIR' in st_name:
name = st_name.split(' CIR')[0]
return name
As a start, I don't have an exact answer for you, but a suggestion to try. I was thinking you could use Replace, but put "" instead of what was used (see thread):
Python - using Replace in Field Calculator
That said, I'm sure some of the experienced Python folks have a more elegant solution, so I'm curious to see what they come up with.
Chris Donohue, GISP
That's what I was looking for jskinner-esristaff, I just needed to declare a variable and use the .split() function. Thanks!
Wouldn't this return unwanted results if the street name started with CIR? For example: W CIRCLE VIEW DR
I'm doing something similar and I use str.rpartition()
head, sep, tail = st_name.rpartition(" CIR")
if tail == '':
return head
If the tail partition string is empty, you know there is nothing else after the separator and it's safe to return the head.
Edit:
Also tagging https://community.esri.com/community/developers/gis-developers/python?sr=search&searchId=08a40ef2-e9...