Sequential Numbering based on last numbering

2295
11
06-29-2018 08:40 AM
CCWeedcontrol
Occasional Contributor III

I have a road layer that i need to add sequential numbering to, the RD_ID field. Some already have a RD_ID number and some are missing. I need to populate attributes of only the missing ones that only have CC in front of them but in sequential order but starting from the last number left off at. I attached a picture of what i need. thanks.

I have the following that adds sequential ID but it doesn't take in consideration where the last RD_ID left off.

import arcpy

fc = r'C:\Temp\Roads.shp'

startNumber = 0

with arcpy.da.UpdateCursor(fc, "RD_ID") as cursor:
    for row in cursor:
            row[0] = startNumber
            startNumber = startNumber + 1
            cursor.updateRow(row)

print 'Done'
0 Kudos
11 Replies
CCWeedcontrol
Occasional Contributor III

I add an if statement but the null and blanks one don't get updated sequentially. I get no error's, i did add "=" to line 26.

RD_ID += 1

import arcpy

fc = r'C:\Temp\Address_Points.shp'

#arcpy.CalculateField_management(fc, "RD_ID", '""" """', "PYTHON")

rd_list = []          
with arcpy.da.SearchCursor(fc, ["RD_ID"]) as cursor:          
    for row in cursor:  
        try:          
            if "CC" in row[0]:                
                rd_list.append(int(row[0].strip("CC")))             
        except TypeError:          
            pass                  
del cursor          
 
              
rd_list.sort()          
RD_ID = rd_list[-1] + 1          
    
#whereclause = "RD_ID = '%CC%'"
with arcpy.da.UpdateCursor(fc, "RD_ID") as rows:           
    for row in rows:
        if row[0] in (""," ", None):
            row[0] = 'CC%05d' %RD_ID 
            RD_ID += 1                      
            rows.updateRow(row)      
    del row           


print 'Done'    
0 Kudos
forestknutsen1
MVP Regular Contributor

They are not getting updated at all? Or they are not getting the right values when updated? Can you set some breakpoints around the if statement and see the behavior?

0 Kudos