Arcpy-Check a row for hyphens

2218
10
Jump to solution
03-03-2016 11:49 AM
DianneGray
New Contributor

Hi Everyone,

I'm trying to do something fairly simple with Python (ArcGIS 10.3): populate a field called StreetNum with street addresses taken from a field called StreetAddr.  However, some street addresses have a one-digit unit number surrounded by spaces before the unit number (e.g. 3 - 560 Main St) and I want only the street number to appear in the StreetNum field.  So in other words, I want to populate StreetNum based on criteria: if there is a hyphen present in the string for a particular row in StreetAddr, then calculate the StreetNum by skipping the first 4 values of StreetAddr; otherwise, use the values of StreetAddr the way they are.

I've tested the cases and they work.  However, I cannot come up with the proper criteria to check for a hyphen in a given row (no matter how I change my criteria, the script either passes or fails all rows).  How would I correctly word my loop criteria?

# Insert Search Cursor into StreetAddr field

rows = arcpy.SearchCursor(fc,["StreetAddr"])

# Check for hyphens in this field

for row in rows:

    if "-" in str(row):

        #Calculate the field by removing the unit number and hyphen from StreetAddr

        arcpy.CalculateField_management(in_table=fc, field="StreetNum", expression="Mid( [StreetAddr],5 )", expression_type="VB", code_block="")

    else:

        #Calculate the field by taking the content from StreetAddr

        arcpy.CalculateField_management(in_table=fc, field="StreetNum", expression="[StreetAddr]", expression_type="VB", code_block="")

del rows

del row

Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JakeSkinner
Esri Esteemed Contributor

In the last dialog box, where you have !StreetAddr!, you will need to call the function you created in the dialog box above.  You should have the following:

calc(!StreetAddr!)

View solution in original post

0 Kudos
10 Replies
JakeSkinner
Esri Esteemed Contributor

Hi Dianne,

You can accomplish this using the Field Calculator.  Ex:

Pre-logic script code:

def calc(field1):
  if "-" in field1:
    return field1.split(" ", 2)[-1]
  else:
    return field1

StreetNum=

calc(!StreetAddr!)

Be sure 'Python' is checked at the top of the field calculator:

Screen1.PNG

DianneGray
New Contributor

Hi Jake,

Can you please explain the syntax of split?  What do the numbers 2 and -1 correspond to? 

I tried using that section of code, but Field Calculator ended up calculating all records to match StreetAddr (in other words, nothing passed the test).  Why would this be the case?  There are two records that have unit numbers, and all I want is the street numbers without the unit number (e.g. 7 - 364 Lougheed Rd should become 364 Lougheed Rd) (I realize my wording above might have been a bit confusing).  I know I could fix this manually but for acquiring more Python experience I'd like to know how to address this with Python.  What else could you suggest?

0 Kudos
DanPatterson_Retired
MVP Emeritus

if it were as simple as one case, why don't you examine the following conceptually

>>> x

'3 - 560 Main St'

>>> "- " in x

True

>>> y = x.split("- ")[1]

>>> y

'560 Main St'

Note, I am checking for "dash space" not just "dash"

If found (True), then split on the aforementioned and take the first fiddly bit (counting from 0... ergo the last piece of the split

JakeSkinner
Esri Esteemed Contributor

The split method will split the string on a space (" ").  The '2' in this syntax is telling the method to stop after the 2nd space is found.  For example, take the following address:

7 - 364 Lougheed Rd

If I were to run the following:

text = '7 - 364 Lougheed Rd'
print text.split(" ", 2)

The following list would be returned:

['7', '-', '364 Lougheed Rd']

The '-1' in the syntax is going to return the last value in the list.  In this case, it will return '364 Lougheed Rd'. 

If you want to upload a sample of the data, I could take a look at it.

DianneGray
New Contributor

Kelowna Addresses.JPG

So here are the addresses that get calculated into StreetNum directly from StreetAddr. No splitting was done for the hyphenated fields when I copied your above code as follows:

0 Kudos
JakeSkinner
Esri Esteemed Contributor

In the last dialog box, where you have !StreetAddr!, you will need to call the function you created in the dialog box above.  You should have the following:

calc(!StreetAddr!)

0 Kudos
DianneGray
New Contributor

Oh wow, it worked!  That was easy! Thanks!

0 Kudos
WesMiller
Regular Contributor III

Have you thought about using the Standardize Addresses—Help | ArcGIS for Desktop tool and joining the table and calculating the desired results.

JoshuaBixby
MVP Esteemed Contributor

I second Wes's comment, especially since the tool is available at all licensing levels.  There are also several Python-based packages that have similar functionality.  I personally like usaddress, but I only work with US addresses.