2

I've got a python script that runs on the Field Calculator of a ESRI File Database. Right now it runs correctly in that it will increment the last three digits of the value. But this is a script that is run as part of post processing so it will be run everytime there are new entries in the feature class. My question is how can I modify this so that every time it runs it will look at the last four digits of the FacilityID and increment from there below is a screenshot of my attribute table:

enter image description here

EDIT: And below is my Python script I need to modify.

cur = arcpy.SearchCursor("Telecommunications Line")
fac_id_values = [int(row.FACILITYID[-4:]) for row in cur]

maximum_value = max(fac_id_values)
if maximum_value:
 rec= maximum_value 
else:
 rec = 0

def autoIncrement(): 
 global rec 
 pStart = 1  
 pInterval = 1 
 if (rec == 0):  
  rec = pStart  
 else:  
  rec += pInterval  
 return "COMLI" + "11" +  format(rec, '03d')

Ok after running the code above I got this as a result. It overwrote the previous values as shown above. The First item should have a FacilityID of COMLI111001. enter image description here

1 Answer 1

7

You can loop through the FC first and find them maximum value.

Example:

import arcpy
nullvalues = ["", " ", None, 0]
# Loop through and get Max value
cur = arcpy.SearchCursor("Telecommunications Line")
fac_id_values = [int(row.FACILITYID[-4:]) for row in cur if row.FACILITYID not in nullvalues]

max_value = max(fac_id_values)

# Loop through and Calculate new values
cur = arcpy.UpdateCursor("Telecommunications Line")
for row in cur:
    max_value = max_value + 1
    if row.FACILITYID in nullvalues:
        row.FACILITYID = "COMLI1" + str(format(max_value,'04d'))
        cur.updateRow(row)

This should ensure that you only update Null values.

12
  • list comprehensions are awesome! May 9, 2014 at 18:23
  • Yes, this could all be put into 1 line, but for readablity, i chose to seperate it into 3. max_value = max([int(row.FACILITYID[-4:]) for row in arcpy.SearchCursor("YOUR FC")])
    – dklassen
    May 9, 2014 at 18:27
  • I'm always a fan of readability, and the list comprehension allowed you to take what would be even more verbose language, compress it to a single line, and still end up with three highly readable lines. Features like these make me enjoy working with Python after toiling for too long with Javascript ;) May 9, 2014 at 18:31
  • Totally agree!!
    – dklassen
    May 9, 2014 at 18:37
  • So I was going to implement this so if Maximum_value is not null then set rec=maximum_value right?
    – yams
    May 9, 2014 at 19:17

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.