AnsweredAssumed Answered

Arcpy-Check a row for hyphens

Question asked by a00840236 on Mar 3, 2016
Latest reply on Mar 4, 2016 by a00840236

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

Outcomes