2

I have a script which reads a geodatabase table and calculate two fields based on existing fields. This is my code:

import arcpy
from arcpy import env

#overwrite output
arcpy.env.overwriteOutput = True

#Linear reference
def locateFeatures(fc,rts,tbl):
    arcpy.LocateFeaturesAlongRoutes_lr(fc, rts, 'RID', '0.1 Meters', tbl, 
    'RID LINE FMEASN TMEASN')

#add field Diff MEAS
def addFieldDif(tbl):
    arcpy.AddField_management(tbl,'DifFMEAS', 'DOUBLE')
    arcpy.AddField_management(tbl,'DifTMEAS', 'DOUBLE')

#calculate Diff MEAS
def calFieldDifMeas(fc):
    fdList = ['FMEASN','TMEASN','FMEAS','TMEAS','DifFMEAS','DifTMEAS']
    with arcpy.da.UpdateCursor(fc,fdList) as cursor:
        for row in cursor:
            row[4] = abs(row[0] - row[2])
            row[5] = abs(row[1] - row[3])
            cursor.UpdateRow(row)

fc = r'C:\Users\Gary\Documents\ArcGIS\Default.gdb\PplProt'
rts = r'C:\Users\Gary\Documents\ArcGIS\Default.gdb\PplRoute'
tbl = r'C:\Users\Gary\Documents\ArcGIS\Default.gdb\Locate_MEAS'
locateFeatures(fc,rts,tbl)
addFieldDif(tbl)
calFieldDifMeas(fc)
print'done!!'   

When I run the script I get the error:

Traceback (most recent call last):
File ", line 31, in
File "", line 21, in calFieldDifMeas
RuntimeError: A column was specified that does not exist.

Maybe this is a very silly error but I've started learning Python recently and I cannot find a fix anywhere. Can anybody give me a hint of what might be causing the error?

  • @AlexTereshenkov I have printed all fields and they all seem to be there. This is the error message: Traceback (most recent call last): File "<module1>", line 31, in <module> File "<module1>", line 21, in calFieldDifMeas RuntimeError: A column was specified that does not exist. – AtlantisNaranja Apr 11 '17 at 12:47
  • @AlexTereshenkov thanks, this helped me to identify what I was doing wrong. I was actually selecting wrong fc. – AtlantisNaranja Apr 11 '17 at 13:42
4

The error RuntimeError: A column was specified that does not exist. usually comes from the cursor when you supply in the list of input fields a field that does not exist.

There are some things you can do for troubleshooting:

  • Try printing all the fields in the feature class before using da.UpdateCursor using arcpy.ListFields():

Code:

for f in fdList: 
  print f, f in [field.name for field in arcpy.ListFields(fc)] 

This should be done before creating a cursor. You should get True for all fields.

  • Make sure you are using the correct feature class - check what feature class the variable fc refers to.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.