I am trying to use an insert cursor to populate several fields with attributes and also the SHAPE field with a multi-part polyline, but I keep getting a "can only concatenate tuple (not "list") to tuple" error. I don't understand, because it works to insert just the SHAPE surrounded by square brackets, and my row from an outer SearchCursor works as a list everywhere else! (Converting to a tuple doesn't work either!).
Here is my code:
# Insert the current superstring into the output feature class insertCursor2 = arcpy.da.InsertCursor('SuperStrings_temp_out', dissolveFields + ["SHAPE@"]) multipartPolyline = arcpy.Polyline(superstringPartsArray) insertCursor2.insertRow(row + [[multipartPolyline]]) del insertCursor2
'dissolveFields' is the list of fields whose attributes I want to update with 'row', which is the current row of a table of unique combinations (from the summary statistics tool), which has the same fields as in the dissolveFields list.
Please help!
Solved! Go to Solution.
Thanks, managed to solve it though.
Turns out converting to a tuple did work, I just had to keep the square brackets around multipartPolyline - although actually what I ended up doing was instead converting 'row' to a list as I had a few other fields to add and it made sense to only do one conversion to list rather than multiple to tuple!
Now my code looks like this:
# Insert the current superstring into the output feature class insertCursor2 = arcpy.da.InsertCursor('SuperStrings_temp_out', dissolveFields + ["FREQUENCY", "SUM_SHAPE_Length", "SHAPE@"]) multipartPolyline = arcpy.Polyline(superstringPartsArray) insertCursor2.insertRow(list(row) + [count] + [multipartPolyline.length] + [multipartPolyline]) del insertCursor2
I just don't understand how 'row', which I treat like a list with no problems earlier on in the code, has suddenly become a tuple!
EDIT: Ok I think I understand a bit better after rereading, I think what you are trying to do should work, however I have had issues doing it like that! My implementation wasnt how I wanted it to be, but went like this:
(Code may only work with standard InsertCursor, not the .da version!)
# Insert the current superstring into the output feature class cursorFieldList = dissolveFields cursorFieldList.append("SHAPE@") insertCursor2 = arcpy.da.InsertCursor('SuperStrings_temp_out', cursorFieldList) #Create empty template row object insertRow = insertCursor2.newRow() multipartPolyline = arcpy.Polyline(superstringPartsArray) #Set geometry insertRow.shape = multipartPolyline #Set other fields insertRow.setValue(field1, row.getValue(field1)) insertRow.setValue(field2, row.getValue(field2)) insertCursor2.insertRow(insertRow) del insertCursor2, insertRow
Thanks, managed to solve it though.
Turns out converting to a tuple did work, I just had to keep the square brackets around multipartPolyline - although actually what I ended up doing was instead converting 'row' to a list as I had a few other fields to add and it made sense to only do one conversion to list rather than multiple to tuple!
Now my code looks like this:
# Insert the current superstring into the output feature class insertCursor2 = arcpy.da.InsertCursor('SuperStrings_temp_out', dissolveFields + ["FREQUENCY", "SUM_SHAPE_Length", "SHAPE@"]) multipartPolyline = arcpy.Polyline(superstringPartsArray) insertCursor2.insertRow(list(row) + [count] + [multipartPolyline.length] + [multipartPolyline]) del insertCursor2
I just don't understand how 'row', which I treat like a list with no problems earlier on in the code, has suddenly become a tuple!
You are using a plus sign in your fields list which means it's trying to add them together. Change to
insertCursor2 = arcpy.da.InsertCursor('SuperStrings_temp_out', [dissolveFields, "SHAPE@"])
Would that not add 'dissolveFields' as a list element within the list?
I hope this link works InsertCursor—Help | ArcGIS for Desktop please see the examples at the bottom of the page
EDIT:
You would create a field list like below:
field_list = [field1,field2,field3]
When you use the plus you are adding or concatenating:
1+1 =2
"value" + "othervalue" = "valueothervalue"