3

I am trying to insert Field Values to an empty table.

My code by so far:

arcpy.CreateTable_management(r"C:\Users\esri\Documents\ArcGIS","test")
arcpy.AddField_management("test","total","LONG")
arcpy.AddField_management("test","incomplete","LONG")
arcpy.AddField_management("test","completed","LONG")
arcpy.AddField_management("test","notstarted","LONG")
list=[1,2,3,4]
ins=arcpy.da.InsertCursor("test","*")
ins.insertRow(list)

It shows the error, the sequence must be matched. I am also tried with

ins.insertRow([list])
  • 4
    I would start by narrowing down your fields, there will be more fields than the ones you've created.. so ins=arcpy.da.InsertCursor("test",["total","incomplete","completed","notstarted"]) but then you will need to del ins later or put your insert cursor in a with block. BTW your table is an Info table, is that what you're after? I think it would be better to arcpy.CreateTable_management(r"C:\Users\esri\Documents\ArcGIS","test.dbf") to use dBase IV as the table type as they are a bit more flexible than an info table. – Michael Stimson Jun 25 at 5:24
2

The source of your problem is as @MichaelStimson alludes to is to do with the fields. What is your insert cursor returning?

Firstly as @MichaelStimson states you are creating an INFO table, probably best to work with a File GeoDatabase Table or dbf. But lets stick with the INFO table.

Also your code is flawed in that you are not setting a workspace so the path to the table is invalid for the AddField tool. It's best practise to get the result object of a tool and extract the output as shown below:

import arcpy
arcpy.env.overwriteOutput = True
resObj = arcpy.CreateTable_management(r"C:\scratch","test")
tbl = resObj.getOutput(0)
arcpy.AddField_management(tbl,"total","LONG")
arcpy.AddField_management(tbl,"incomplete","LONG")
arcpy.AddField_management(tbl,"completed","LONG")
arcpy.AddField_management(tbl,"notstarted","LONG")

with arcpy.da.InsertCursor(tbl,"*") as ins:
        print(ins.fields)

Use the da cursor they are much faster. If you call the property fields on the cursor this is what you get:

(u'Rowid', u'OBJECTID', u'FIELD1', u'TOTAL', u'INCOMPLETE', u'COMPLETED', u'NOTSTARTED')

So there are 3 fields you are not accounting for when provide your list of 4 numbers. Gary's code works because he is defining the fields to be returned in the cursor, in his case the four he adds so he avoids the problem of dealing with those auto-generated fields.

So converting your output to a dbf format I would use this code:

import arcpy
arcpy.env.overwriteOutput = True
resObj = arcpy.CreateTable_management(r"C:\scratch","test.dbf")
tbl = resObj.getOutput(0)
arcpy.AddField_management(tbl,"total","LONG")
arcpy.AddField_management(tbl,"incomplete","LONG")
arcpy.AddField_management(tbl,"completed","LONG")
arcpy.AddField_management(tbl,"notstarted","LONG")

myRow = (1,2,3,4) # Note this is a tuple not a list
fields = ["total","incomplete","completed","notstarted"]
with arcpy.da.InsertCursor(tbl,fields) as ins:
    ins.insertRow(myRow)
3

This works as stand alone it may work in python window as well,

import arcpy

arcpy.env.overwriteOutput = True
arcpy.env.addOutputsToMap = False
table = arcpy.CreateTable_management(r"C:\Users\larry d gester\Documents\ArcGIS\Default.gdb","test")

fieldDict = {"total":"LONG","incomplete":"LONG","completed":"LONG","notstarted":"LONG"}
fields = ["total","incomplete","completed","notstarted"]

for k,v in fieldDict.iteritems():
    arcpy.AddField_management(table,k,v)

editor = arcpy.da.Editor(r"C:\Users\larry d gester\Documents\ArcGIS\Default.gdb")
editor.startEditing(True,True)
editor.startOperation()
with arcpy.da.InsertCursor(table,fields) as ic:
    row = [1,2,3,4]
    ic.insertRow(row)

try:
    del ic

except:
    pass

editor.stopOperation()
editor.stopEditing(True)

try:
    del editor
except:
    pass

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.