Brain cramp with an Insert Cursor

863
5
05-18-2018 02:43 PM
JoeBorgione
MVP Emeritus

I'm developing a script that creates a table adds fields and then populates the values of those fields.  I'm good with creating the table and adding the fields, it's the insert cursor that's giving me grief.  (Perhaps I'm giving the insert cursor grief; it's friday afternoon after all...)

Let's call the table newTable, I have list of the values for each of the fields in order:

# the list values looks like this:
[101, u'Salt Lake County', u'Jordan School District', u'South Jordan City', u'South Salt Lake Valley Mosquito Abatement District', u'Jordan Valley Water Conservancy District', u'Jordan/Canyons Debt Service Area', u'Central Utah Water Conservancy District', u'Crescent Cemetery District', u'South Valley Sewer District']‍‍

 However,  when I try to use the list as the source, it errors out with TypeError: sequence size must match size of the row.

I can see where the issue is: when I perform arcpy.ListFields(newTable) I get 11 fields.  But, but the length of my list of values is 10: the difference being the objectid.  I don't quite understand how to accommodate for the OID and the subsequent difference in row size vs the number of list elements.

That should just about do it....
0 Kudos
5 Replies
JoeBorgione
MVP Emeritus

Got it....  And late on Friday afternoon....  Here you go:

 

I originally opened the insert cursor like this:

cursor = arcpy.da.InsertCursor(newTable,'*')

Which makes available all fields for the insert cursor including the OID. My solution is to create a list of fields with the arcpy.ListFields() function, pop the first element off that list and then write the name values of the weird list objects.  It's that last list I then pass to the insert cursor and my lengths are all good:

myFields = arcpy.ListFields(newTable) # create a list of list objects
x = myFields.pop(0)    #gets rid of the oid
fields = []
for f in myFeilds:
  fields.append(f.name) # gets all the field names into the new list call fields

cursor = arcpy.da.InsertCursor(newTable,fields)  #open a cursor for all fields,sans OID
cursor.insertRow(values) # add the values from my original post above
del cursor


‍‍‍‍‍‍‍‍‍‍‍
That should just about do it....
0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Glad you got it sorted out.  I strongly discourage the use of asterisks for field column mapping with cursors.  In talking with Esri product teams, there was some debate about not allowing them for performance, maintainability, and troubleshooting reasons; but convenience took the day.

Whether using asterisks or not, if you have OID as a field with your insert cursor, just pass it None and it will auto populate.

JoeBorgione
MVP Emeritus

Okay- passing it None makes sense too. And yes, I agree after today's lesson, '*' is just looking for trouble!

Thanks!

Joshua Bixby

That should just about do it....
0 Kudos
DanPatterson_Retired
MVP Emeritus

line 1, 2  myFields check line 4 ... OCD day

myFields = arcpy.ListFields(newTable) # create a list of list objects
x = myFields.pop(0)    #gets rid of the oid
fields = []
for f in myFields:
  fields.append(f.name) # gets

# ---- or to reduce spelling errors

fields = [f.name for f in arcpy.ListFields(newTable)][1:]  # ---- drop the 1st field
JoeBorgione
MVP Emeritus

HAHAHA.  I noticed that too but gave it a what the heck it's Friday!  Like minds....  

That should just about do it....
0 Kudos