Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

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
share|improve this question

2 Answers 2

up vote 7 down vote accepted

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
share|improve this answer
    
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 '13 at 21:51

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)
share|improve this answer
    
+1, Clever user of a generator-iterator! –  blah238 Apr 8 '13 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 '13 at 15:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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