In this thread, I am creating a dictionary with which to use with an InsertCursor to add rows to an existing table. I can get the dictionary created okay, but I keep getting an error when I try to use it as the data source:
Traceback (most recent call last):
File "<ipython-input-4-ce36707b33ba>", line 3, in <module>
cursor.insertRow(row)
TypeError: sequence size must match size of the row
In another thread, Randy Burton suggests iterating though a dictionary so I'm attempting to follow his lead.
My dictionary looks like this :
{'16304760420000': (1536639.422987029, 7422834.91819194),
'16304760430000': (1536594.1241931021, 7422763.591562942),
'16304760440000': (1536511.8395807743, 7422768.880266264),
'16304760450000': (1536457.3127869368, 7422770.376654357)}
I fiddled around a bit with the dictionary itself, but didn't get too far, so after taking another look at the online help for InsertCursors, I turn the dictionary into a list and tried that:
bucket = r'J:\PythonDevelopment\ParcelsXY_Mojo\ParcelsMojo.gdb\LocalSlcoDept_ParcelsXY'
bucketFields = ['parcel_id','XCOORD','YCOORD']
row_values = list(myDictionary.items())
cursor = arcpy.da.InsertCursor(bucket,bucketFields)
for row in row_values:
cursor.insertRow(row)
I think my problem with this is the help page describes using the row_values list to insert a @SHAPE, but I just want to insert a row in a table where row[0] us the parcel_id, row[1][0] is the XCOORD and row[1][1] is the YCOORD.
How do I translate myDictionary key:(value1,value2) or my row_values items into a form that the InsertCursor can handle?
Solved! Go to Solution.
He could just change up row_values:
row_values = [(k,)+v for k,v in d.items()]
This part of the error message is most telling:
TypeError: sequence size must match size of the row
It is saying you are passing a sequence (list, tuple, etc...) with the wrong number of elements to the cursor.
Looking at your code, your cursor has 3 fields but your dictionary contains items with only 2 items per tuple.
You might try:
d = {'16304760420000': (1536639.422987029, 7422834.91819194),
'16304760430000': (1536594.1241931021, 7422763.591562942),
'16304760440000': (1536511.8395807743, 7422768.880266264),
'16304760450000': (1536457.3127869368, 7422770.376654357)}
for k in d.keys(): # something like this for lines 7-8 in your code
print k, d[0], d[1] # parcel_id, X, Y
Thanks Randy- I'll give it a spin.....
He could just change up row_values:
row_values = [(k,)+v for k,v in d.items()]
Now I have a couple of options. Thanks Joshua!