1

With Arctoolbox and python scripting I'm creating long tool, but one of the steps is to select by attributes with SQL expression. Let's imagine that:

featureCar = arcpy.GetParameterAsText(0) is Data Type/Feature Set: attributes weight, length, height

filter = arcpy.GetParameterAsText(1) is Data Type/SQL Expression.

output = arcpy.GetParameterAsText(2)

In addition, when I run a tool from arctoolbox and previously specify in sql expression:

"weight" > 2000 AND "length" > 2 AND "height" > 1 from featureCar with assistance of Query Builder

I'm expecting that this query as it is will be passed to arcpy.SelectbyAttribute_management, but it seems not. How to pass this expression correctly to arcpy.SelectbyAttribute_management in python script? I've tried:

arcpy.SelectbyAttribute_management(featureCar, "NEW_SELECTION", "(filter)"), and similar solutions with quoting filter, but I always get ERROR 000358: Invalid expression. I've noticed that is something with '\'{}\''.format(filter), but don't know how.

3

A few things...

  • filter is a reserved name in Python (it's a builtin function) so you shouldn't use that as a variable name.
  • your where clause is already a string as you are using GetParameterAsText() to return it as a string, so no need to put it inside quotes. You should try something like this:

reference your where clause like this:

where = arcpy.GetParameterAsText(1)

and pass it in like this:

arcpy.SelectbyAttribute_management(featureCar, "NEW_SELECTION", where)

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.