Field Calculate Sequential Numbers starting with specific 18 digit number

4580
29
Jump to solution
05-09-2016 07:41 AM
anayoung
New Contributor III

Hello,

I am need to field calculate sequential numbers starting with 033900000100191511. So, the numbering would be 033900000100191511, 033900000100191512, 033900000100191513 etc...

Can anyone help me with a python script for this?

With much appreciation,

Ana

0 Kudos
1 Solution

Accepted Solutions
DanPatterson_Retired
MVP Esteemed Contributor

Ok... that is an ugly one, since you are working with strings so the simplest thing is to increment from a know easy starting point and concatenate the string to it.

try these fixes

pstart = 511

return "033900000100191" + str(rec)

can't test it, so just select a few rows and try it and report back with a screen grab of the table if possible

View solution in original post

29 Replies
DanPatterson_Retired
MVP Esteemed Contributor

What is the field type?

What do you have as a script so far?

anayoung
New Contributor III

Hi,

It is a String fieldThe script I found on the field calculator help:Parser:

Python

Expression:

autoIncrement()

Code Block:

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

return rec

DanPatterson_Retired
MVP Esteemed Contributor

Ok... that is an ugly one, since you are working with strings so the simplest thing is to increment from a know easy starting point and concatenate the string to it.

try these fixes

pstart = 511

return "033900000100191" + str(rec)

can't test it, so just select a few rows and try it and report back with a screen grab of the table if possible

anayoung
New Contributor III

Hi Dan,

I've been trying the code but haven't been successful yet. Either it needs something different or our network problems are interfering with the calculation. I have attached a screen shot showing the field calculator with the adjusted code you provided below:

SeqNumbering.PNG

0 Kudos
DanPatterson_Retired
MVP Esteemed Contributor

you need to enable the code block check box and set your python parser to python.

you don't include the Code block line because what follows is the code block

in the Expression box, you put in    autoIncrement()  which is the name of the script you using

DanPatterson_Retired
MVP Esteemed Contributor

PS

check the documentation on using code blocks in the examples section

Calculate Field examples—Help | ArcGIS for Desktop

0 Kudos
AbdullahAnter
Regular Contributor II

Dan,I think this expression will calculate wrong number if the records more than 489.

because it will add a new digit and the total digits for record will be 19 digit , but Ana Young want 18 digit.

So, you should to know the count of all records that Ana have, to write the expression.

JoshuaBixby
MVP Esteemed Contributor

Using a generator expression and string formatting, the number of records doesn't need to be known ahead of time:

Expression:

next(g)

Code Block:

def gen(x):
    while True:
        yield "{:0=18}".format(x)
        x+=1
g = gen(33900000100191511)  #or replace with different starting number (not string of number)
AbdullahAnter
Regular Contributor II

Yes. using your expression ,Joshua. let us not need to know number of records.

Good job, Joshua.

0 Kudos