Selecting multiple attributes from the same column using where_clause

04-20-2017 07:13 PM
New Contributor II


   I am really green in this, but I am having trouble using where_clause to select two types two different types of attributes within the same column using the Select(analysis) tool with arcpy. 

The way I  do it regularly using Select by Attributes windows is the following 


DrainClass = 'Somewhat excessively drained' OR DrainClass = 'Well drained'


I have tried using double quotations on the outside, but I keep getting an ExecuteError


ExecuteError: ERROR 000358: Invalid expression DrainClass = 'Somewhat excessively drained' OR DrainClass = 'Well drained'


Does anyone know how to set the correct expression in arcpy?

Thank you

0 Kudos
6 Replies
MVP Esteemed Contributor

It is helpful if you can post the exact code you are using to call the function/tool.  Post a couple examples if you have tried them and they all fail.

MVP Esteemed Contributor

DrainClass Like %drained%'  if there are only 2

Title = 'Actor' OR Title = 'Advisor'     good... no typing involved, select the field, the OR and the case

(Title = 'Actor') OR (Title = 'Advisor')   better, no typing, paying attention that conditionals are surround in ( )'s

New Contributor II

Thank you,  

    I ran the code using the Select (analysis) tool, then copied the python code snippet from the results window and this is what it said 

where_clause1=""""DrainClass" = 'Well drained' OR "DrainClass" = 'Somewhat excessively drained'"""

greatSoiloutput = "greatSoils.shp"where_clause1=""""DrainClass" = 'Well drained' OR "DrainClass" = 'Somewhat excessively drained'"""arcpy.Select_analysis(bigSoils, greatSoiloutput, where_clause1)


0 Kudos
New Contributor II

Sorry I am very new at this.

0 Kudos
MVP Esteemed Contributor
greatSoiloutput = "greatSoils.shp"
where_clause1 = """
"DrainClass" = 'Well drained' OR "DrainClass" = 'Somewhat excessively drained'
arcpy.Select_analysis(bigSoils, greatSoiloutput, where_clause1)

When formatted so it is easier to read.

So line 1... is the input shapefile, however, the path to the shapefile is not specified, so there has to be a reference to an environment variable (ie ...   arcpy.env.workspace = r"c:\folder\subfolder")

line 2 and 4 ... contain triple double quotes to encase/enclose the code block shown (separated on its own line to facilitate reading).  The actual code block uses the format for shapefiles, etc,  Field names are enclosed in double quotes and text conditions on the right of the condition check are enclosed in single quotes.

Esri Esteemed Contributor

For simplicity reasons it would be better to use the IN operator:

"DrainClass" IN ('Well drained', 'Somewhat excessively drained')