AnsweredAssumed Answered

Alternative for Wildcards in Update Cursor

Question asked by dondailey on Apr 20, 2016
Latest reply on Apr 22, 2016 by dondailey

I've got a point feature class of road intersections in which I used a script to create a field of joined road names from a line feature class. I've got to separate each road name into separate fields. I've been able to do that without much problem. But the road names were joined in more or less random fashion and now I need to rank the road names based on their type into their own field i.e. Interstates in the first field, US Highways in the second, state highways in the third, etc. All the highways begin with either Interstate, US or State.

 

I've tried doing a wildcard if ... else on the list created by the .split inside the update cursor but apparently that's not allowed. From searching it seems that regular expressions (which I've never used) might be my only hope. Anybody have any suggestions?

 

The code I used to split the field and place the road names into individual fields.

 

 

try:
# Import arcpy module
    from arcpy import env

#set workspace
    env.workspace = r"GDB"

# Local variables:
    fc = r"feature class"
    
#create update cursor to place values in fields
    fieldList = ["AllRoutes","ID_Road1_Cross","ID_Road2_Cross","ID_Road3_Cross",
                 "ID_Road4_Cross","ID_Road5_Cross"]
    with arcpy.da.UpdateCursor(fc,fieldList) as cursor:
        for row in cursor:
              cleaned = [x.strip() for x in row[0].split("&")]
              if len(cleaned) == 2:
                row[1] = cleaned[0]
                row[2] = cleaned[1]
              elif len(cleaned) == 3:
                row[1] = cleaned[0]
                row[2] = cleaned[1]
                row[3] = cleaned[2]
              elif len(cleaned) == 4:
                row[1] = cleaned[0]
                row[2] = cleaned[1]
                row[3] = cleaned[2]
                row[4] = cleaned[3]
              elif len(cleaned) == 5:
                row[1] = cleaned[0]
                row[2] = cleaned[1]
                row[3] = cleaned[2]
                row[4] = cleaned[3]
                row[5] = cleaned[4]
              cursor.updateRow(row)


except arcpy.ExecuteError:
    print arcpy.GetMessage(2)

except:
    print "Process did not complete"

Outcomes