Alternative for Wildcards in Update Cursor

3947
12
Jump to solution
04-20-2016 02:53 PM
DonDailey
New Contributor

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"
0 Kudos
1 Solution

Accepted Solutions
DarrenWiens2
MVP Honored Contributor

It's getting to the end of a long day and I'm having a hard time deciphering the problem on this one, so bear with me. If you want to know if a string starts with something, you can use the startswith() method:

if myString.startswith('Interstate'):
  return 'Interstate'

View solution in original post

12 Replies
DanPatterson_Retired
MVP Emeritus

I formatted your code Code Formatting... the basics++

DonDailey
New Contributor

Thanks for that Dan. I originally didn't see any way to do the code correctly.

0 Kudos
DarrenWiens2
MVP Honored Contributor

It's getting to the end of a long day and I'm having a hard time deciphering the problem on this one, so bear with me. If you want to know if a string starts with something, you can use the startswith() method:

if myString.startswith('Interstate'):
  return 'Interstate'
DonDailey
New Contributor

It looks like this is going to work. Thanks for the help Darren.

0 Kudos
curtvprice
MVP Esteemed Contributor

What happens when two interstates intersect? Can Field1 and Field2 both be interstates?

If so I suggest (inside the cursor with block) assigning a key and sorting, like this:

cleaned = [x.strip() for x in row[0].split("&")]  

rds = []
for rd in cleaned:
    rdup = rd.upper()
    if rdup[0] == "I": key = 0  # Interstate
    elif rdup[0] == "U": key = 1 # US route
    else: key = 2 # state and other
    rds.append([key, rd])
# sort and place in fields
for k, rd in enumerate(sorted(rds)): 
    row[k+1] = rd[1] # k = 0,1,2..
rows.updateRow(row)
DarrenWiens2
MVP Honored Contributor

Just small edits:

- should be:

for rd in cleaned:

- and

elif rdup[0] == "U": key = 1 # US route 

curtvprice
MVP Esteemed Contributor

Thanks Darren. Very much appreciate that - hate to have broken code posted!

0 Kudos
DanPatterson_Retired
MVP Emeritus

Just curious... how many records are there in the road dataset to begin with?

0 Kudos
DonDailey
New Contributor

I'm testing this out for a single rural county that has about 1,290 intersections on all public roads. I'm sure to do all counties it will be over 100,000 records. Right now I'm just trying to prove it can be done.

0 Kudos