0

I would like to create a query in ArcPy without creating a temporary additional attribute first. Unfortunately I fail with the syntax for creating the expression. I want to check if a ratio of two existing attributes (RCT_WIDTH_MBG_Length_REWI and RCT_WIDTH_MBG_Width_REWID) falls below a certain value.

My latest code is:

    expression = '!RCT_WIDTH_MBG_Length_REWI! / !RCT_WIDTH_MBG_Width_REWID! > 3' 
    arcpy.SelectLayerByAttribute_management('Clean_LYR', 'NEW_SELECTION', expression)
    if int(arcpy.GetCount_management('Clean_LYR')[0]) > 0:
        print(str(int(arcpy.GetCount_management('Clean_LYR')[0])) + ' werden geloescht')
        arcpy.DeleteFeatures_management('Clean_LYR')

and during the script run I code the error message:

ExecuteError: ERROR 000358: Invalid expression

0

1 Answer 1

0

As commented by @Vince:

The "bang sandwich" only applies to Calculate Field (at the interface with Python). The SQL where_clause must be valid SQL (which means valid SQL identifiers -- just column names, no delimiter characters).

If your data source is a file geodatabase feature class or a shapefile, instead of using:

expression = '!RCT_WIDTH_MBG_Length_REWI! / !RCT_WIDTH_MBG_Width_REWID! > 3'

try using:

expression = 'RCT_WIDTH_MBG_Length_REWI / RCT_WIDTH_MBG_Width_REWID > 3'
1
  • It does depend on the data source, though -- Sometimes double-quotes are required, and sometimes double-quotes are optional, and sometimes its a bracket pair ([,]) instead. But it's never exclamation points ('bang' in Unix parlance).
    – Vince
    Commented Dec 6, 2021 at 23:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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