6

I want to autoincrement values in a field within a Python script. I've done this using Field Calculator and the code referenced here. I tried using this code in the Python script below but it is not working.

#Import modules
import os, sys
import arcpy

#Set variables
workspace = "D:\\GIS\\data.gdb"
arcpy.env.workspace = workspace
table_name = "data_table" 
field = "KEY_2" #Short integer field

#This is the function I want to use on my KEY_2 field
rec=0 
def autoIncrement(field): 
    global rec
    pStart = 10  
        pInterval = 2 
    if (rec == 0):  
        rec = pStart  
        return rec 
    else:  
        rec += pInterval  
        return rec

field = autoIncrement()

I get a TypeError when I run this code:

Traceback (most recent call last):
  File "C:\Python26\ArcGIS10.0\pythonwin\pywin\framework\scriptutils.py", line 312, in     RunScript
    exec codeObject in __main__.__dict__
  File "D:\Python\scratch\sc3.py", line 23, in <module>
   field = autoIncrement()
TypeError: autoIncrement() takes exactly 1 argument (0 given)

I've moved things around in the script a bit but have not yet gotten my field to populate. Anyone have a suggestion?

Here's my final code, based on blord-castillo's answer:

#Import modules
import os, sys
import arcpy

#Set variables
workspace = "D:\\GIS\\data.gdb"
arcpy.env.workspace = workspace
table_name = "data_table" 
field = "KEY_2" #I made this a text field
rec=0

def autoIncrement(pStart = 10,pInterval = 2): #Using default values instead of setting     inside function
    global rec
    if (rec == 0):  
        rec = pStart  
        return rec
    else:
        rec += pInterval
        return rec

incrementCursor = arcpy.UpdateCursor(table_name) #There is no guarantee of order here
for row in incrementCursor:
    row.setValue(field, str(autoIncrement()))
    incrementCursor.updateRow(row)

del incrementCursor

3 Answers 3

10

You should use a cursor instead of field calculation to achieve this.

import arcpy

#Set variables
rec=0
workspace = "D:\\GIS\\data.gdb"
arcpy.env.workspace = workspace
table_name = "data_table" 
field = "KEY_2" #Short integer field

cursor = arcpy.UpdateCursor(table_name) #There is no guarantee of order here
for row in cursor:
    row.setValue(field,autoIncrement())
    cursor.updateRow(row)

def autoIncrement(pStart=10,pInterval=2): #Using default values instead of setting inside function
    global rec
    if (rec == 0):  
        rec = pStart  
        return rec
    else:
        rec = rec + pInterval
        return rec
1
  • Cool, thank you very much, @blord-castillo. I'm posting my final code above as based on what you suggest. I just had to switch location of cursor and function, and define the rec variable. Thanks, again!
    – Patty Jula
    Apr 5, 2013 at 21:51
6

Here's a simple auto incrementor that doesn't rely on global variables

def autoIncrement(start=0,step=1):
    i=start
    while 1:
        yield i
        i+=step

incrementCursor = arcpy.UpdateCursor(table_name) #There is no guarantee of order here
incrementer = autoIncrement(10,2)
for row in incrementCursor:
    row.setValue(field, incrementer.next()) #Note use of next method
    incrementCursor.updateRow(row)

Batteries included!!! Apparently this is already in the standard library...

import itertools

inc = itertools.count()
incrementCursor = arcpy.UpdateCursor(table_name) #There is no guarantee of order here
for row in incrementCursor:
    row.setValue(field, inc.next()) #Note use of next method
    incrementCursor.updateRow(row)
2
  • +1, Clever user of a generator-iterator!
    – blah238
    Apr 8, 2013 at 3:18
  • Thank you for the response, @Luke. I'll give this a try, it's good for me to work more with while loops.
    – Patty Jula
    Apr 8, 2013 at 15:18
0

Looks like an old thread BUT...

There is the ability to sort with the UpdateCursor. I used the following

incrementCursor = arcpy.UpdateCursor(table_name, "", "", "", "SORT FIELD")

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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