Selecting multiple attributes from the same column using where_clause

1609
6
04-20-2017 07:13 PM
CristianLa_Rosa1
New Contributor II

Hello

   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
JoshuaBixby
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.

DanPatterson_Retired
MVP Emeritus

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

CristianLa_Rosa1
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
CristianLa_Rosa1
New Contributor II

Sorry I am very new at this.

0 Kudos
DanPatterson_Retired
MVP Emeritus
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.

XanderBakker
Esri Esteemed Contributor

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

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