I'd like to create a field of sequential numbers starting at 001 counting all the way until 322. Every number must be 3 digits. How do I do this?
Thank you all for your help.
Solved! Go to Solution.
Hi James,
You can do this using the Field Calculator. Here is an example:
rec=0
def autoIncrement():
global rec
pStart = 1 #adjust start value, if req'd
pInterval = 1 #adjust interval value, if req'd
if (rec == 0):
rec = pStart
else:
rec = rec + pInterval
if len(str(rec)) == 1:
return "00" + str(rec)
elif len(str(rec)) == 2:
return "0" + str(rec)
elif len(str(rec)) == 3:
return str(rec)
Hi James,
You can do this using the Field Calculator. Here is an example:
rec=0
def autoIncrement():
global rec
pStart = 1 #adjust start value, if req'd
pInterval = 1 #adjust interval value, if req'd
if (rec == 0):
rec = pStart
else:
rec = rec + pInterval
if len(str(rec)) == 1:
return "00" + str(rec)
elif len(str(rec)) == 2:
return "0" + str(rec)
elif len(str(rec)) == 3:
return str(rec)
Thanks Jake. That's perfect!
Another approach, instead of using global variables, is to use a generator:
Code Block:
def gen(x=0):
while True:
yield "{:0=3}".format(x)
x+=1
g = gen()
Expression:
next(g)