Select to view content in your preferred language

Arcpy-Check a row for hyphens

2961
10
Jump to solution
03-03-2016 11:49 AM
DianneGray
Deactivated User

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
10 Replies
DianneGray
Deactivated User

This is helpful generally, but the US address locator styles don't appear to recognize the hyphenated address prefixes.  These addresses are Canadian

0 Kudos