0

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?

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Nov 28, 2018 at 14:51
1
  • Each iteration of addField only has 2 elements. The references to addfield[2] and above are what are causing the IndexError. Commented Nov 28, 2018 at 21:01

1 Answer 1

4

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])
answered Nov 28, 2018 at 16:42
1
  • I went for the second option and that seemed to do it, thank you. Commented Nov 29, 2018 at 8:24

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.