3

I would like to fill a column in my attribute table with an string that ends in a numeric value.

Example:

  • Object001
  • Object002
  • Object003

I would like to automate the process of filling in the attribute table.

There is a great post on Adding automated values to attribute field? made previously that explains how to add numbers in series to an attribute table. I would like to build on this code by adding a string invfront of the number.

I'm trying to combine the python code found in the above post with a string but it isn't working. See below for the code which will add a series.

    Expression:
autoIncrement()

Expression Type:
PYTHON_9.3

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

However when I try and combine the code in the field like below, I just get an error.

'Object'autoIncrement()   

**or**  

'Object' & autoIncrement()

My syntax is wrong but I'm not quite sure how to correct it.

  • "Object"+str (autoincrement ()).zfill (3). Replace 3 with 4 if you have more than 1000 and less than 9999 records – FelixIP Dec 16 '14 at 22:39
3

We can adjust the Field Calculator Code Block to achieve the effect you are looking for. The following assumes that your target field is a Text Field, and has enough length to hold the value produced by the Field Calculation. For example, if we want to prefix our incremental count with the word "Object":

Expression:
autoIncrement("Object")

Expression Type:
PYTHON_9.3

Code Block:
rec=0
def autoIncrement(string_prefix):
 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 string_prefix + str(rec)

This process should yield the following results into your target Text Field:

"Object1"
"Object2"
... and so on

Adding the "Object 001" (the leading zeros) will be a little more complicated, but it is possible. You would need to find a way to write the leading zero's based on the total number of records in the table, for example, if the table has 5000 records, you would need something like "Object 0001" and so on...

  • Jim Thank you so much for your response. I ended up solving this a slightly different way before I found your response. Finding a response from geogeek about number padding [See Here] I was able to use this code in the code block and then I added the syntax that @FelixIP suggested in the field below the pre-logic script code box which was: 'Object' + str(autoIncrement()) Thanks for the help. [1] [1]:gis.stackexchange.com/questions/81316/… – Dre Dec 17 '14 at 15:49
2

Solved this using a combination of everyone's suggestions:

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 str(rec).zfill(3)

In the Field Expression -

 'Object' + str(autoIncrement())

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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