AnsweredAssumed Answered

KeyError when working with fields

Question asked by vshah6 on Mar 13, 2018
Latest reply on Mar 13, 2018 by vshah6

I get KeyError: 5 error when running my script tool.

 

Background of the script:

Earlier in the script, i do a summary statistics and I am editing the fields to get the field alias so the output looks cleaner because later in the script, table to excel is run to export the table. Here is what my script looks like:

 

fieldList = arcpy.ListFields(in_table)
length = len(fieldList)


arcpy.DeleteField_management(in_table, nodeIDField)

alias_dict = {1: 'Network ID', 2: 'Project ID', 3: 'Municipality', 4: 'Node Count'}

for i, field in enumerate(fieldList):
    if field.type != 'OID':
        newfield = '{}new'.format(field.name)
        arcpy.AddField_management(in_table, newfield, field.type, field.precision,
                                field.scale, field.length, alias_dict[i], field.isNullable, field.required, field.domain)
        arcpy.CalculateField_management(in_table, newfield, '!{}!'.format(field.name), "PYTHON_9.3")
        arcpy.DeleteField_management(in_table, field.name)

 

When running this, the existing fields do get modified with the alias I want for each, but then get KeyError: 5 error. My thought is that it is because the key 5 does not exist. How do I get the script to just pass through to the next portion of the script if the key of 5 does not exist?

Outcomes