arcpy.da.insertCursor w/ dictionary as input...

5341
8
Jump to solution
06-18-2012 12:44 PM
KevinBell
Occasional Contributor III
I'm trying to pump the key and value of a dictionary through a da.insertCursor and I'm floundering...

the dictionary is structured like this:

(420957.9933000002, 4510316.955): [51217.156,
                                    59376.688,
                                    93876.453,
                                    127022.82,
                                    163947.11,
                                    148067.66,
                                    135979.39,
                                    108321.33,
                                    67035.406,
                                    45043.16,
                                    37991.52,
                                    36629.535,
                                    115.57169,
                                    110.87349,
                                    175.09225,
                                    238.25777,
                                    330.70523,
                                    322.0,
                                    329.24261,
                                    276.23621,
                                    191.93385,
                                    136.51129,
                                    112.5774,
                                    86.31971]}

and my cursor code looks like so:

print '\n' pprint.pprint(allData)     fieldList = ['SOL1', 'SOL2', 'SOL3', 'SOL4',             'SOL5', 'SOL6', 'SOL7', 'SOL8',              'SOL9', 'SOL10', 'SOL11', 'SOL12',             'DUR1', 'DUR2', 'DUR3', 'DUR4',             'DUR5', 'DUR6', 'DUR7', 'DUR8',             'DUR9', 'DUR10', 'DUR11', 'DUR12']  fc = r'C:\gis\solarTESTING\default.gdb\points' c = arcpy.da.InsertCursor(fc,('SHAPE@XY', fieldList))  for k,v in allData.iteritems():     row =(k,v)     c.insertRow(row)  del c 


What am I doing wrong here?!
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
JasonScheirer
Occasional Contributor III
Need to add the lists together, otherwise it's nested lists in lists and that's not acceptable input.

fieldList = ['SOL1', 'SOL2', 'SOL3', 'SOL4',             'SOL5', 'SOL6', 'SOL7', 'SOL8',              'SOL9', 'SOL10', 'SOL11', 'SOL12',             'DUR1', 'DUR2', 'DUR3', 'DUR4',             'DUR5', 'DUR6', 'DUR7', 'DUR8',             'DUR9', 'DUR10', 'DUR11', 'DUR12']  fc = r'C:\gis\solarTESTING\default.gdb\points' with arcpy.da.InsertCursor(fc,['SHAPE@XY'] + fieldList) as c:     for k,v in allData.iteritems():         row =  + v         c.insertRow(row)

View solution in original post

0 Kudos
8 Replies
KevinHibma
Esri Regular Contributor
Whats the actual error?

Just a guess, should you have a comma instead of a colon in your dictionary?
(420957.9933000002, 4510316.955): [51217.156,
or
(420957.9933000002, 4510316.955), [51217.156,
0 Kudos
KevinBell
Occasional Contributor III
Whats the actual error?

Just a guess, should you have a comma instead of a colon in your dictionary?
(420957.9933000002, 4510316.955): [51217.156,
or
(420957.9933000002, 4510316.955), [51217.156,


The XY tuple is the dictionary key, and the stuff in the brackets is the dict value, which is a list, so the colon belongs and is stock dictionary structure.  They should be passed to the cursor without the colon via "row = (k, v)" I think.
0 Kudos
KevinHibma
Esri Regular Contributor
Right, but what I'm saying is I'm not too sure that the cursor will take a dictionary as input....
From the help:
insertRow (row)A list or tuple of values. The order of values must be in the same order as specified when creating the cursor.


I havent personally worked a whole lot with the new cursors, but I stick with straight lists when working with them. Heres snippets from 2 scripts I've written.

Polygons...
cursorFC = arcpy.da.InsertCursor(fc, ["SHAPE@", "EventDate", "Scale", "InvScale", "Width", "Height"])
polygonGeo = arcpy.Polygon(shapeArray)
cursorFC.insertRow([polygonGeo, eventDateTime, eventScale, eventInvScale, eventWidth, eventHeight]) 


or
Points
rowsDA = da.InsertCursor(outFC, ['Name', 'Descript', 'Type', 'DateTimeS', 'Elevation', 'SHAPE@X', 'SHAPE@Y', 'SHAPE@Z'])
rowsDA.insertRow([trkPoint.name, trkPoint.desc, trkPoint.gpxtype, trkPoint.t,
                              trkPoint.z, trkPoint.x, trkPoint.y, trkPoint.z])


EDIT:
I see what you're saying about the unpacking now... whats the error you're getting?
0 Kudos
KevinBell
Occasional Contributor III
TypeError: 'field_names' must be string or non empty sequence of strings
File "C:\gis\solarTESTING\MultiRasterToAttributedPoints_3.py", line 108, in <module>
  c = arcpy.da.InsertCursor(fc,('SHAPE@XY', fieldList))
0 Kudos
KevinBell
Occasional Contributor III
Kh:  Thanks for the code sample!  This doesn't seem to be as clean but it worked:

c = arcpy.da.InsertCursor(fc,('SHAPE@XY', 'SOL1', 'SOL2', 'SOL3', 'SOL4',
            'SOL5', 'SOL6', 'SOL7', 'SOL8', 
            'SOL9', 'SOL10', 'SOL11', 'SOL12',
            'DUR1', 'DUR2', 'DUR3', 'DUR4',
            'DUR5', 'DUR6', 'DUR7', 'DUR8',
            'DUR9', 'DUR10', 'DUR11', 'DUR12'))

for k,v in allData.iteritems():
    #row =(k,v)
    row =(k,v[0],v[1],v[2],v[3],v[4],v[5],v[6],v[7],v[8],
          v[9],v[10],v[11],v[12],v[13],v[14],v[15],v[16],
          v[17],v[18],v[19],v[20],v[21],v[22], v[23])
    c.insertRow(row)

del c 
print 'DONE'

0 Kudos
KevinHibma
Esri Regular Contributor
Ugly 🙂
Function >  prettiness?

I''ll post if I can find out why you cant pass your list of fields in like you were trying.
0 Kudos
JasonScheirer
Occasional Contributor III
Need to add the lists together, otherwise it's nested lists in lists and that's not acceptable input.

fieldList = ['SOL1', 'SOL2', 'SOL3', 'SOL4',             'SOL5', 'SOL6', 'SOL7', 'SOL8',              'SOL9', 'SOL10', 'SOL11', 'SOL12',             'DUR1', 'DUR2', 'DUR3', 'DUR4',             'DUR5', 'DUR6', 'DUR7', 'DUR8',             'DUR9', 'DUR10', 'DUR11', 'DUR12']  fc = r'C:\gis\solarTESTING\default.gdb\points' with arcpy.da.InsertCursor(fc,['SHAPE@XY'] + fieldList) as c:     for k,v in allData.iteritems():         row =  + v         c.insertRow(row)
0 Kudos
KevinBell
Occasional Contributor III
^^^well that's much better!  Thanks!
0 Kudos