Select to view content in your preferred language

SelectLayerByAttribute_management - Correct syntax for expression

9179
13
03-22-2012 06:39 PM
KatrinaBennett
Emerging Contributor
I'm having some trouble running ArcGIS 10 Python scripting.

I am trying to select the attributes in my layer based on a number of expressions. I don't think I have the Syntax correct. Although, I've followed what is posted here: http://help.arcgis.com/en/arcgisdesktop/10.0/help/index.html#//001700000071000000.

This is the portion of my code that is giving me the error.


sel_pts_lyr is a feature layer, and the join I used to generate the data fields was done using env.qualifiedFieldNames = False.

I am running this in gp = arcgisscripting.create(9.3).

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

  #Select Analysis
  print "Attempting Select"
  expression1 = ' \"ELEV\" <= \"Elev_metre \" + 100 AND \"ELEV\" >= \"Elev_metre\" - 100 '
  expression2 = ' \"SLP\" <= \"mean_slp\"+ \"sd_slp\" AND \"SLP\" >= \"mean_slp\" - \"sd_slp\" '
  expression3 = ' \"ASP\" <= \"mean_asp\" + \"sd_asp\" AND \"ASP\" >= \"mean_asp\" - \"sd_asp\" '

  arcpy.SelectLayerByAttribute_management(sel_pts_lyr, "NEW_SELECTION", expression1)
  arcpy.SelectLayerByAttribute_management(sel_pts_lyr, "SUBSET_SELECTION", expression2)
  arcpy.SelectLayerByAttribute_management(sel_pts_lyr, "SUBSET_SELECTION", expression3)


ERROR OUTPUT

Traceback (most recent call last):
  File "C:\data2\smart_clip2\model_clip.py", line 91, in <module>
    arcpy.SelectLayerByAttribute_management(sel_pts_lyr, "NEW_SELECTION", expression1)
  File "C:\Program Files (x86)\ArcGIS\Desktop10.0\arcpy\arcpy\management.py", line 4259, in SelectLayerByAttribute
    raise e
ExecuteError: ERROR 000358: Invalid expression
Failed to execute (SelectLayerByAttribute).


~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~


Any assistance would be very appreciated!!


Thank you,

Katrina
13 Replies
AdelItani
Emerging Contributor
Hy Katrina,
Just a notice:
did you include the next line:
import arcpy  # or argisscripting
# ...
0 Kudos
BruceNielsen
Frequent Contributor
Terry,

This should work for you:
arcpy.SelectLayerByAttribute_management("Faults_2", "NEW_SELECTION", "\"names2\" = 'Wabash Valley liquefaction features'")

Field names in a GeoDB need to be surrounded in double quote, not brackets.
0 Kudos
TonyEnberg
Deactivated User
I am having problems with a similar piece of Python code to Terry's but my field is long integer and value is a variable.

Using Terry's code could anyone show me the correct syntax if the field value is a variable of long integer type?

I have found a few examples via ESRI and other sites but non seem to work for me. Data is in a file GDB. ArcGIS version 10.

Thanks

Tony Enberg
0 Kudos
BruceNielsen
Frequent Contributor
If the field type is integer, you don't enclose the expected value in single quotes. That allows you to simplify the expression even more by using the single quotes to encompass the entire expression. Use Python's string substitution to insert the value of the variable:
arcpy.SelectLayerByAttribute_management("FCname", "NEW_SELECTION", '"integerField" = %i' % variableName)