Select to view content in your preferred language

ArcPro Calculate Field with Python Error

551
5
Jump to solution
01-28-2025 09:58 AM
StephanieOosterhouse
Emerging Contributor

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!

StephanieOosterhouse_0-1738087076725.png

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#

0 Kudos
1 Solution

Accepted Solutions
Nick_Creedon
Frequent Contributor

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)

 

View solution in original post

5 Replies
DavidPike
MVP Notable Contributor

It's probably just an Arcism if it worked before.  Easiest fix might be to just put everything into the code block return.

0 Kudos
DanPatterson
MVP Esteemed Contributor
# 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'

... sort of retired...
StephanieOosterhouse
Emerging Contributor

The expression is now valid but I am getting this error: 

StephanieOosterhouse_0-1738093468526.png

This seems like an easy fix but I am not sure where/how to change the int to string. Thank you for your help!

0 Kudos
Nick_Creedon
Frequent Contributor

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)

 

StephanieOosterhouse
Emerging Contributor

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!!