0

I'm using an insert cursor to write rows to a destination feature class where the input Shapefile field names do not match the destination FC names. To accomplish this, I'm trying to use the indices of the input shape field list to input to specific fields. Running into a "List indices must be integers not tuple" error when executing and have tried all combinations of brackets, parentheses, etc. I don't think I'm seeing the error right:

outfc = #the input shapefile from earlier in code
destsfp = #the destination featureclass from earlier in code

allrowvalues = arcpy.ListFields(outfc)
rowvalues = allrowvalues[[2],[3],[5],[6],[7],[8],[9],[10],[11],[12],[13],[14]]
#open insert cursor
cursor = arcpy.da.InsertCursor(destsfp,('SHAPE@X','SHAPE@Y','SHAPE@Z','GRADIENT','BEARING','HEIGHT_IN_METRES','WIDTH_IN_METRES','LENGTH_IN_METRES','DEPRESSION_DEPTH','SYMBOLOGY','FEATURE_NAME','FEATURE_DESC'))
#insert new rows that have the above fields
for row in rowvalues:
    cursor.insertRow(row)
del cursor
8
  • 2
    What line is giving the error (presumably for row in rowvalues?)? Did you try taking the brackets off of the values in rowvalues? I think the first thing I'd try would be a list of numbers [2, 3, 5, 6, 7 ...] or maybe this way rowvalues = ([2], [3], [5] ...). I'm not really even comfortable with inserting rows in a given place. I've only ever inserted rows as needed (not in a loop). Commented Sep 10, 2014 at 15:42
  • allrowvalues doesn't contain what you think it contains if I'm understanding your intention. ListFields() will return a list of field objects from the input feature class. It does not actually return the values in those columns.
    – MWrenn
    Commented Sep 10, 2014 at 15:47
  • Also, your error is in the fact that you are using a list improperly. if you want to create a subset list of certain field names from allrowvalues you'd have to do it like rowvalues = [allrowvalues[2].name, allrowvalues[3].name, ...]
    – MWrenn
    Commented Sep 10, 2014 at 15:50
  • rowvalues = allrowvalues... is the line giving the error. I did retry with no brackets, just commas. didn't work either. I think MWrenn is right - I need to retry by creating a subset list of the fieldnames from rowvalues.
    – tay bro
    Commented Sep 10, 2014 at 15:50
  • However, keep in mind my other comment. All rowvalues is going to be is a list of field names at those indexes. It will not actually be values from outfc at all. Your insertRow line will fail next because you are not inserting row objects which is what insertRow expects, since row will just be a string (the name of a column in outfc)
    – MWrenn
    Commented Sep 10, 2014 at 15:56

1 Answer 1

3

based on all the comments, try something like this -

field_indexes_to_copy = [2, 3, 5, 6, 7, 8, 9, 10, 11, 12, 13 ,14]
search_cursor = arcpy.da.SearchCursor(outfc, "*")
insert_cursor = arcpy.da.InsertCursor(destsfp,('SHAPE@X','SHAPE@Y','SHAPE@Z','GRADIENT','BEARING','HEIGHT_IN_METRES','WIDTH_IN_METRES','LENGTH_IN_METRES','DEPRESSION_DEPTH','SYMBOLOGY','FEATURE_NAME','FEATURE_DESC'))

for row in search_cursor:
    insert_cursor.insertRow([row[i] for i in field_indexes_to_copy])

I haven't run this code myself, but I think that or something similar should work.

4
  • I think close but am running into the object has no attribute getValue? Thanks for your help, MWrenn - huge help. Runtime error Traceback (most recent call last): File "<string>", line 108, in <module> AttributeError: 'tuple' object has no attribute 'getValue' line 108 is insert_cursor.insertRow([row.getValue(f)..
    – tay bro
    Commented Sep 10, 2014 at 17:50
  • Edited my answer. arcpy.da.SearchCursor returns a tuple, not a row object like the old arcpy.SearchCursor returns. I'm still on Arc 10.1, so I haven't used the da module at all.
    – MWrenn
    Commented Sep 10, 2014 at 17:54
  • Works perfect with one change: search_cursor=arcpy.da.SearchCursor(outfc,"*"). Needed the 2nd argument in SearchCursor. Solved it - thank you.
    – tay bro
    Commented Sep 10, 2014 at 18:07
  • Cool. I edited my answer to include the "*" param for anybody else looking at it.
    – MWrenn
    Commented Sep 10, 2014 at 18:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.