As part of a larger project i'm looking to add multiple fields to a shapefile using an index. This is the snippet in question:
listFields = [['ITEM', 'TEXT'], ['SYSTEM', 'TEXT'], ['WATERCOURCE_BANK', 'TEXT'], ['AIMS_REF', 'TEXT'],
['CHAINAGE', 'TEXT'], ['GRID_REF', 'TEXT'], ['CUT', 'TEXT'], ['LENGTH_KM', 'TEXT'], ['COMMENTS', 'TEXT']]
print("Adding fields")
for addField in listFields:
print(" ...{}".format(addField[0]))
arcpy.AddField_management('table_stamp', addField[0], addField[1], addField[2], addField[3], addField[4],
addField[5], addField[6], addField[7], addField[8])
I'm getting the following error, pointing at the line of code above:
IndexError: list index out of range
As far as I can tell I have got my Indexing right. Nine fields to add so the indexes should be 0 to 8.
Anybody have any idea what the issue might be?
1 Answer 1
Add field only adds one field at a time. You will need to add an AddField for each field. Something like...
arcpy.AddField_management('table_stamp', addField[0][0], addField[0][1])
arcpy.AddField_management('table_stamp', addField[1][0], addField[1][1])
or
for item in listffields:
arcpy.AddField_management('table_stamp', item[0], item[1])
-
I went for the second option and that seemed to do it, thank you.Matt Houston– Matt Houston2018年11月29日 08:24:31 +00:00Commented Nov 29, 2018 at 8:24
addField
only has 2 elements. The references toaddfield[2]
and above are what are causing the IndexError.