I am trying to use the field calculator to create an alphanumeric sequence, I tried to write a statement but I don't know what is wrong.
So my sequence would be in a field called "Field_number" in the attribute table with my desired sequential values in format (1A001, 1A002....1A999...1B001, 1B002...1B999...1C001). (Total record of 3215)The sequence numbers will range from 001 to 999, and the first two letters will follow the pattern 1A, 1B, 1C, and so on.
After creating my sequence in my Field_number Field, I then need to combine 2 attribute table together. But I am stuck on creating the Alphanumeric sequence field called "Field_number". Final look should be SPLCAR1A001....
Here's my code below: Pre-Logic Script Code:
rec = 0
letter = "A"
def autoIncrement():
global rec, letter
pStart = 1
pEnd = 999
pInterval = 1
if rec < pEnd:
rec += pInterval
else:
rec = pStart
letter = chr(ord(letter) + 1)
return "1{}{}".format(letter, str(rec).zfill(3))
And autoIncrement() enter image description hereTO generate
-
What does Python in your title have to do with your question when you appear to be trying to write VB script? Personally, I would do this using Python and eliminate VB script from your question. You had a Python Script Tool tag but do not appear to be writing that so I've removed it.PolyGeo– PolyGeo ♦2023年09月13日 01:03:16 +00:00Commented Sep 13, 2023 at 1:03
3 Answers 3
Using @Hornydd 's recommendation. You need to feed the "Creata" field into the script.
You script should read:
def autoIncrement(Creata):
creata = Creata
rec = 0
letter = "A"
pS = 1
pE = 5
pI = 1
if rec < pE:
rec += pI
else:
rec = pS
letter = chr(ord(letter) + 1)
return "{}1{}{}".format(creata,letter, str(rec).zfill(3))
Your initiator the "Field_Number =" part should bring in the "Creata" field should read:
autoIncrement(!Creata!)
Firstly you are calculating into the numeric field Field_number
so you would never be able to create your code as that is text. You need to be calculating into a text field. Secondly you don't pass into your function the other field Creata
to do the concatenation that you desire. Suggest you spend some quality time looking at the examples in the help file to understand syntax.
-
Yes, I am aware i have a new Text field called Text_ID, that i created. I did look into the link above but I none gives an answer to my question of having "my desired sequential values in format (1A001, 1A002....1A999...1B001, 1B002...1B999...1C001)".Jmerryline– Jmerryline2023年09月12日 21:48:08 +00:00Commented Sep 12, 2023 at 21:48
-
But you explicitly state in your question you want SPLCAR1A001, now you are asking 1A001, so which is it? If you want SPLCAR to be the start of your code you need to pass that field into the function and concatenate.Hornbydd– Hornbydd2023年09月12日 22:03:51 +00:00Commented Sep 12, 2023 at 22:03
-
Yes, I do want my final field to be SPLCAR1A001 and continuous. I have the field of Creata (SPLCAR) already and now I want my Field_number (1A001) to have the beginning letters of 1A, 1B and so on (1A001, 1A002....1A999...1B001, 1B002...1B999...1C001). Which i would then Concatenate to a new Text field called Text_ID. and then finally have this (SPLCAR1A001) sequence. But im stuck on this part (1A001, 1A002....1A999...1B001) has i only have what the picture shows (1-999). Do you get me now. Thanks.Jmerryline– Jmerryline2023年09月13日 13:31:21 +00:00Commented Sep 13, 2023 at 13:31
Figured it out. I didnt need my Creata and Field_number fields anymore. Thank You.
Here's the code Below.
rec = 0
letter = "A"
def autoIncrement():
global rec, letter
pStart = 1
pEnd = 999
pInterval = 1
if rec < pEnd:
rec += pInterval
else:
rec = pStart
letter = chr(ord(letter) + 1)
return "{}{}{}".format("SPLCAR1", letter, str(rec).zfill(3))
autoIncrement()
Explore related questions
See similar questions with these tags.