'Cannot find field' error with arcpy.da.SearchCursor

2075
11
03-29-2022 10:08 AM
DavidHeacock
New Contributor II

Hi there - I have some scripts I've been trying to use that I haven't run in several months. There have been updates to Pro/Arcpy since then and now they are throwing errors, so I'm trying to get to the bottom of all  them.

I'm getting the "Cannot find field 'PARCEL_ID'" error coming from the code:

 

 

with arcpy.da.SearchCursor(templayer3, ['PARCEL_ID', "Acres"]) as cursor:
                for row in cursor:

 

 

I've checked for any typos, exported versions of 'templayer3' throughout the script and 'PARCEL_ID' is in there correctly formatted, without aliases, etc. Are there alternative reasons why one might get the 'Cannot find field' error?

Thanks

0 Kudos
11 Replies
JayantaPoddar
MVP Esteemed Contributor

What if you replace single quotes with double quotes?

"PARCEL_ID"

Also you might want to copy-paste the fieldname rather than typing it down.



Think Location
0 Kudos
DavidHeacock
New Contributor II

Double quotes don't work either (that's how they were before actually, the single quotes were just an attempt at trying to resolve the bug)

0 Kudos
DanPatterson
MVP Esteemed Contributor

print the results of ListFields just before the with statement

ListFields—ArcGIS Pro | Documentation


... sort of retired...
0 Kudos
DavidHeacock
New Contributor II

The feature class outputs the correct fields. Placed before the problematic cursor:

field_names_output = [f.name for f in arcpy.ListFields(templayer3)]
            arcpy.AddMessage(field_names_output)

 

Outputs:

['OID', 'Shape', 'PARCEL_ID', 'Acres']

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

There are 2 ways to run the searchcursor.

with arcpy.da.SearchCursor(templayer3, ["PARCEL_ID", "Acres"]) as cursor:
    for row in cursor:
        blah
# --- or -----
cursor = arcpy.da.SearchCursor(templayer3, ["PARCEL_ID", "Acres"]):
for row in cursor:
    blah

'Cannot find field' error with arcpy.da.SearchCurs... - Esri Community

If the approach yields the same issue AND it is a table in a file geodatabase and not some other variant of a table, then I would escalate to tech support, since you seem to have confirmed that the table and the field exist.

 


... sort of retired...
0 Kudos
DavidHeacock
New Contributor II

Thanks, I didn't have luck with either of those approaches. 'templayer3' is in memory, but I tried converting to a feature class in a gdb and I'm getting the same result.

0 Kudos
DominicRoberge2
Occasional Contributor III

what is you try something like this:

 

fields = ['PARCEL_ID', 'Acres'] 

with arcpy.da.SearchCursor(templayer3, fields) as cursor: 
     for row in cursor: 
        print(u'{0}, {1}'.format(row[0], row[1]))
0 Kudos
DavidHeacock
New Contributor II

getting the same error declaring the fields as a list

0 Kudos
JayantaPoddar
MVP Esteemed Contributor

In case you haven't tried,

1. Close all applications.

2. Shut down your machine.

3. Get yourself a cup of coffee.

4. Start your system.

5. Start ArcGIS Pro, and give it another try.



Think Location