Error 000539: Invalid Field

2002
2
Jump to solution
03-19-2018 01:23 PM
VishalShah2
Occasional Contributor II

Hello,

So I am first running a summary statistics to get the information I need and then the next step of the tool is to modify the table fields so I can get the field alias I want. This is done by creating new fields, doing calculations, etc. Here is my code:

import arcpy
import os
import arcpy.mapping as map
from arcpy import env
import datetime

arcpy.env.overwriteOutput = True

fiberFeatures = r'sdm_Fiber Segments'
date = datetime.date.today()
reportsGDB = r'C:\Users\vshah\Desktop\Test Folder\test_output.gdb'
reportsFolder = r'C:\Users\vshah\Desktop\Test Folder'

inLayer = fiberFeatures
outputName = 'fiber_summary'
outLayer = reportsGDB + "/" + outputName
statsFields = [["Feet", "SUM"], ["Miles", "SUM"]]
caseFields = ["NetworkID", "ProjectID"]

arcpy.Statistics_analysis(inLayer, outLayer, statsFields, caseFields)

in_table = reportsGDB +"\\" + 'fiber_summary'
deleteField = "FREQUENCY"

fieldList = arcpy.ListFields(in_table)
length = len(fieldList) - 2  ##subtracting 2 to account for the field being deleted when the modification is executed and to not count the Object ID field.

arcpy.DeleteField_management(in_table, deleteField)

alias_dict = {1: 'Network ID', 2: 'Project ID', 3: 'Total Feet', 4: 'Total Miles'}

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)‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

After this will be a simple export to excel sheet. However when I get to this portion of the script, I get ERROR 000539: Invalid field FREQUENCY even though it is being deleted before it gets to creating new fields. Any thoughts on how to solve this or any help you can offer?

0 Kudos
1 Solution

Accepted Solutions
JamesMacKay3
Occasional Contributor

You're calling ListFields() and storing the result in fieldList prior to deleting the field.  When you iterate through the fieldList values later, it will still contain FREQUENCY.  Try moving line 28 up to line 24.

View solution in original post

2 Replies
JamesMacKay3
Occasional Contributor

You're calling ListFields() and storing the result in fieldList prior to deleting the field.  When you iterate through the fieldList values later, it will still contain FREQUENCY.  Try moving line 28 up to line 24.

VishalShah2
Occasional Contributor II

Wow, that was such a simple fix... I thought it would be much more complex than that. Thank you so much James!

0 Kudos