1

I have two separate scripts used in Field Calculator for an "ID" attribute field. Script 1 is to create sequential numbers for the field. Script 2 is to add leading zeros to the sequential numbers.

Both scripts work independently, but is there a way to combine both scripts to run together as a single script?

Script 1 (Pre-Logic Script Code)

rec=0
def autoIncrement():

    global rec

    pStart = 4164 #adjust start value, if req'd

    pInterval = 1 #adjust interval value, if req'd

      if (rec == 0): 

        rec = pStart 

     else: 
        rec = rec + pInterval

     return rec

Script 1 (In smaller window)

autoIncrement()

Script 2

!ID!.zfill(7)
1

If you want to replace 4164 with 04164, or 4165 with 04165, then just change:

 return rec

with:

return "0" + str(rec)

or if you need to have 7 places then:

return str(rec).zfill(7)
0

I was looking to populate the field with sequential numbers and then add leading zeros to all the populated numbers. eg. 04164, 04165, and so on. A co-worker and I found a solution by changing the script to:

rec=0
res = 0 
ren = 0

def autoIncrement(): 
 global rec
 global res
 global ren
 pStart = 4164  
 pInterval = 1 

  if (rec == 0):  
   rec = pStart
   res = str(rec)
   ren = res.zfill(7)
  else:  
   rec += pInterval
   res = str(rec)
  ren = res.zfill(7)  

 return ren
1
  • To say thanks please use your upvotes and/or the green accept checkmark. – PolyGeo Feb 11 '20 at 20:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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