Select to view content in your preferred language

'An expected field was not found or could not be retrieved properly' for arcpy.da.SearchCursor where clause

8046
3
Jump to solution
01-23-2018 04:01 PM
TessOldemeyer
Regular Contributor

 I am trying to add a where clause into a arcpy.da.SearchCursor statement, and I keep receiving a 'An expected Field was not found or could not be retrieved properly' error with this code:

where_expression = ' "FIELD2" = "CODE1" or "FIELD2" = "CODE2" '
search_feats = {f[0]:f[1] for f in arcpy.da.SearchCursor(table,["FIELD1","FIELD2","FIELD3"], where_expression)}‍‍

with arcpy.da.UpdateCursor(New_Table,["NT_FIELD_1","NT_FIELD_3"]) as upd_cur:
     for upd_row in upd_cur:
         upd_row[1] = search_feats.get(upd_row[0], 0)
         upd_cur.updateRow(upd_row)  

del upd_cur‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

I believe this error is falling on the where clause, as if I exclude the clause from search_feats, the code runs without error. I have used a where clause successfully in this structure in the past, but not with a text field such as FIELD2 here. 

Any tips or suggestions would be much appreciated. I have tried many different variations of syntax with no luck yet. 

I have verified that the fields exist in the table, have been typed correctly, and do not have any locks.

0 Kudos
1 Solution

Accepted Solutions
RandyBurton
MVP Alum

Try:

where_expression = " FIELD2 = 'CODE1' or FIELD2 = 'CODE2' "‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Field names in a where clause are usually not in quotes.  And with file geodatabase, use outside double quotes; inside text field values in single quotes.

View solution in original post

3 Replies
RandyBurton
MVP Alum

Try:

where_expression = " FIELD2 = 'CODE1' or FIELD2 = 'CODE2' "‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

Field names in a where clause are usually not in quotes.  And with file geodatabase, use outside double quotes; inside text field values in single quotes.

TessOldemeyer
Regular Contributor

That was the ticket! Thank you so much!

0 Kudos
gisstaffnapacounty
Emerging Contributor

I also ran into a problem similar with a different root cause.  The following was returning An expected Field was not found or could not be retrieved properly. [Closure_Segment].


I found that my segId was unintentionally the value of None.  Once a proper value was included no more problems.

where = "OBJECTID={}".format(segId)
cursor = arcpy.da.SearchCursor("Closure_Segment", ["OBJECTID","SHAPE@"], where_clause=where)
for row in cursor:
shp = row[1]