Hello,
I am trying to calculate a field with a constant text string and sequential numbering with leading zeros. I have used this expression and code block previously and been successful but now I am getting an error that it cannot find a function that matches the text string. Obviously, there would not be a function matching my text string, but I am not sure why it is not being recognized as a string with the ' '.
Any help would be appreciated. Thanks!
Expression: 'ARC-DOOR-STN-' + "{:0>5}".format(SequentialNumber())
Code Block:
# Calculates a sequential number
# More calculator examples at esriurl.com/CalculatorExamples
rec=0
def SequentialNumber():
global rec
pStart = 669
pInterval = 1
if (rec == 0):
rec = pStart
else:
rec = rec + pInterval
return rec
#change pstart to last ID number#
Solved! Go to Solution.
I recommend just adding prefix above the sequential code and add it in the return string. This keeps it simple, and I have used it on some hefty datasets. Check out below: you will see I was creating ID's for manholes and their district.
prefix= "6-MH-"
rec=0
def SequentialNumber():
global rec
pStart = 1
pInterval = 1
if (rec == 0):
rec = pStart
else:
rec = rec + pInterval
return prefix + str(rec)
It's probably just an Arcism if it worked before. Easiest fix might be to just put everything into the code block return.
# code block
rec=0
st = 'ARC-DOOR-STN-' + "{:0>5}"
def SequentialNumber():
global rec
pStart = 669
pInterval = 1
if (rec == 0):
rec = pStart
else:
rec = rec + pInterval
rec = st.format(rec)
return rec
# expression
SequentialNumber()
'ARC-DOOR-STN-00669'
The expression is now valid but I am getting this error:
This seems like an easy fix but I am not sure where/how to change the int to string. Thank you for your help!
I recommend just adding prefix above the sequential code and add it in the return string. This keeps it simple, and I have used it on some hefty datasets. Check out below: you will see I was creating ID's for manholes and their district.
prefix= "6-MH-"
rec=0
def SequentialNumber():
global rec
pStart = 1
pInterval = 1
if (rec == 0):
rec = pStart
else:
rec = rec + pInterval
return prefix + str(rec)
With a bit of adjusting I was able to add in the leading zeros using this method as well. Here is the code chunk for anyone else who might need it:
prefix= "ARC-ROOM-STN-"
rec=0
def SequentialNumber():
global rec
pStart = 670
pInterval = 1
if (rec == 0):
rec = pStart
else:
rec = int(rec) + pInterval
rec = "{:05}".format(rec)
return prefix + rec
#pStart equals next number in sequence
Thank you!!