Trying to iterate through the a Layer and assign a list of Unique ID to a Field from attribute table

678
4
09-08-2023 12:43 PM
Labels (3)
JMerryline
New Contributor

Hello, I'm a beginner in ArcPy and I want to perform some tasks using ArcPy but I am not getting results. Using ArcMap I have an attribute field, with like 13K number of records. The Layer is called Splicepoint.

I would like to change the names of a fields in the attribute table called Station ID (to have a sequential name that I have created. In 11 character long name – SPLCNVA1A001, SPLCNVA1A002, SPLCNVA1A003…… SPLCNV1B001….. SPLCNV1Z001….. SPLCNV1Z999) They are all related to the field of SUBTYPE (FOSC). Also, I need to skip I and O’s to avoid confusion .

This is just a one time code that would keep changing the sequence.

Picture1.pngPicture2.png

Tags (1)
0 Kudos
4 Replies
RhettZufelt
MVP Frequent Contributor

How/where are you trying to accomplish this?  In ArcMap with select and calculate field GP tools, or in a python window?

Also, you say sequential name that 'you have created'.  Do you have a separate lookup table or are you wanting it to start with the first FOSC row and append the sequential number to the end of "SPLCNV" for the Station ID field value?

Need some kind of clarification as your examples show different values (other than the sequential number) appended to the end.  ( A1A00#, 1B00#, 1Z00# ) or are these typos as there doesn't seem to be a pattern other than the last three numbers (where are the A's, B's and Z's coming from).

R_

JMerryline
New Contributor

On ArcMap Python window. 

Yes I am starting with " with the first FOSC row and append the sequential number to the end of "SPLCNV" for the Station ID field value?"

okay so the pattern is 

SPLCNV1A001,

SPLCNV1A002,

SPLCNV1A003……

SPLCNV1B001….. all the way to "Z"

SPLCNV1Z001…..

SPLCNV1Z999.... and eventually end at 1Z

SPLCN = SPLICE

NV = STATE which is Nevada

and then 1A and 001 (number up to 999) 

THANK YOU.

0 Kudos
BlakeTerhune
MVP Regular Contributor

@JMerryline wrote:

I would like to change the names of a fields in the attribute table called Station ID (to have a sequential name that I have created.

 Have you already coded the logic in Python to generate this sequential value?

0 Kudos
JohannesLindner
MVP Frequent Contributor

Run this in the Python Window (change the first three lines):

 

fc = "TestPolygons"  # path to the feature class or name of the layer
field = "TextField1" # field you want to edit
sql = "Subtype = 'FOSC'" # select the features you want to edit

import string
letters = list(string.ascii_uppercase)
letters.remove("I")
letters.remove("O")

with arcpy.da.UpdateCursor(fc, field, sql) as cursor:
    for i, row in enumerate(cursor):
        letter = letters[int(i/1000)]
        number = i % 1000
        new_id = "SPLCNV1{}{:03}".format(letter, number)
        cursor.updateRow([new_id])

 

 

This will fail after Z999!


Have a great day!
Johannes