0

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
If this question can be reworded to fit the rules in the help center, please edit the question.

  • So you're looking to have like A01, A02, A03.. like a specific string + auto increment, correct? Or are you looking to just add 1 to existing values? Basically, do the values exist already and you're updating them or do they need created? – Branco Aug 10 '15 at 17:15
  • Yes I am basically using a specific string and autoincrement. – Knteza Aug 10 '15 at 17:22
3

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

FieldName =

"SWCL" + str(autoInc()).zfill(4)

The .zfill() function makes sure your string is at least n characters long and will pad with zeroes.

  • 1
    Branco's exactly right. There are some examples in the ESRI help docs including the example above at this link under "Accumulative and sequential calculations". – John Aug 10 '15 at 21:22
  • @John Thanks for posting that link. I had no idea there was a collection of examples like that. I took the above one from a random code download thing ESRI had. I think it was when they had the scripts to download pages and such. Great resource!! – Branco Aug 11 '15 at 1:52

Not the answer you're looking for? Browse other questions tagged or ask your own question.