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
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.
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)
print the results of ListFields just before the with statement
ListFields—ArcGIS Pro | Documentation
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']
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.
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.
Thank you @DanPatterson! This saved me a lot of extra frustration. My field aliases were using . instead of underscores.
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]))
getting the same error declaring the fields as a list