I am searching for a way to auto increment an ArcGIS field which begins with text and followed by numbers. Ex. SWCL0000. I have tried the auto increment code in the field calculator but once I added text and a starting number, I kept on getting errors.
closed as off-topic by PolyGeo♦ May 27 at 11:10
This question appears to be off-topic. The users who voted to close gave this specific reason:
- "When seeking help to debug/write/improve code always provide the desired behavior, a specific problem/error and the shortest code (as formatted text, not pictures) needed to reproduce it in the question body. Providing a clear problem statement and a code attempt helps others to help you." – PolyGeo
I borrowed a lot of the code from here. Try this.
Turn on the
python parser in the Field Calculator.
Pre-Logic Script Code:
rec = 0 def autoInc(): global rec pStart = 1 pInterval = 1 if (rec == 0): rec = pStart else: rec += pInterval return rec
"SWCL" + str(autoInc()).zfill(4)
.zfill() function makes sure your string is at least n characters long and will pad with zeroes.