Select to view content in your preferred language

Using Python to Split Address Field

3701
4
Jump to solution
10-28-2016 12:59 PM
MatthewUpchurch1
Deactivated User

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.

0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

Hi Matthew,

Try the following:

def street_name (st_name):
    if ' CIR' in st_name:
         name = st_name.split(' CIR')[0]
         return name

View solution in original post

4 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Matthew,

Try the following:

def street_name (st_name):
    if ' CIR' in st_name:
         name = st_name.split(' CIR')[0]
         return name
ChrisDonohue__GISP
MVP Alum

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.

Dan_Patterson 

Darren Wiens 

Chris Donohue, GISP

MatthewUpchurch1
Deactivated User

That's what I was looking for jskinner-esristaff, I just needed to declare a variable and use the .split() function. Thanks!

0 Kudos
BlakeTerhune
MVP Regular Contributor

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...