Select to view content in your preferred language

KeyError when working with fields

3246
3
Jump to solution
03-13-2018 10:50 AM
VishalShah2
Frequent Contributor

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?

0 Kudos
1 Solution

Accepted Solutions
VishalShah2
Frequent Contributor

Never mind. I found a working solution.

nodeIDField = "COUNT_ID"

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

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' and i <= length:
        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)

The original length of the fieldlist was 6, hence why i got KeyError: 5. It was looking for the 5th key in the dictionary. I am subtracting 2 because I do not want to count the ObjectID field or the field I am deleting using arcpy.DeleteField_management.

View solution in original post

0 Kudos
3 Replies
VishalShah2
Frequent Contributor

Never mind. I found a working solution.

nodeIDField = "COUNT_ID"

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

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' and i <= length:
        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)

The original length of the fieldlist was 6, hence why i got KeyError: 5. It was looking for the 5th key in the dictionary. I am subtracting 2 because I do not want to count the ObjectID field or the field I am deleting using arcpy.DeleteField_management.

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

If the field isn't in your alias list, do you want it created with no alias or not even created at all?

0 Kudos
VishalShah2
Frequent Contributor

Not created at all.

0 Kudos