1

I'm trying to code the names from geo database the total records of names are 957 and i want each name to have the uniqueID and it should not exceed three digits. starting from 001.

The below script started from 101 which now is giving me a problem when i rich to one thousand because it gives me the four digits.

I have been using the below Script to code names but unfortunately i'm now stuck because some ID'S are now having four digits.

rec=0 
def autoIncrement(): 
 global rec 
 pStart =101  
 pInterval = 1 
 if (rec == 0):  
  rec = pStart  
 else:  
  rec += pInterval  
 return rec

Here is the steps to code,

Right-Click on Field Calculator Set the Parser to Python (Default is usually VB) Tick Show Codeblock.

Paste the following into the Pre-Logic Script Code

rec=0 
def autoIncrement(): 
 global rec 
 pStart =101  
 pInterval = 1 
 if (rec == 0):  
  rec = pStart  
 else:  
  rec += pInterval  
 return rec

Paste the following code in the smaller box below the Pre-Logic Script Code:

autoIncrement()

Click OK.

  • Please format the code in the appropriate code blocks that you are using. There is a selector {} on the toolbar that allows you to format code blocks. Since python is a space sensitive language, doing this helps eliminate the possibility that you're not indenting lines properly. – Branco Nov 24 '15 at 13:30
1

You need to use the zfill string method. To start at 001 and end at 957, you need to use a text field and do the following in the code block:

rec=0 
def autoIncrement(): 
 global rec 
 pStart = 1  
 pInterval = 1 
 if (rec == 0):  
  rec = pStart  
 else:  
  rec += pInterval  
 return str(rec).zfill(3)

And the expression stays the same:

autoIncrement()

  • Yes! i did it. Thank you very much, i'm now going to create another field for double so that i can transfer those code to double field since i need the codes in number. I appreciate your help. – Napoleon Mudau Madzinge Nov 24 '15 at 14:13

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.