+ Reply to Thread
Results 1 to 14 of 14

Thread: SelectLayerByAttribute_management - Correct syntax for expression

  1. #1
    Katrina Bennett
    Join Date
    Mar 2011
    Posts
    5
    Points
    0
    Answers Provided
    0


    0

    Default SelectLayerByAttribute_management - Correct syntax for expression

    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/arcgisdesk...00000071000000.

    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
    Last edited by kbennett88; 03-22-2012 at 11:06 PM.

  2. #2
    Fabian Blau
    Join Date
    Oct 2011
    Posts
    101
    Points
    60
    Answers Provided
    10


    0

    Default Re: SelectLayerByAttribute_management - Correct syntax for expression

    Hi!

    Try it without escaping the double quotes:

    Code:
    expression1 = ' "ELEV" <= "Elev_metre " + 100 AND "ELEV" >= "Elev_metre" - 100 '
    When mixing the different kinds of quote signs escaping should not be necessary.

  3. #3
    Mike Wissner
    Join Date
    Feb 2012
    Posts
    65
    Points
    1
    Answers Provided
    0


    0

    Default Re: SelectLayerByAttribute_management - Correct syntax for expression

    Hi all,

    I'm also trying to run a SelectLayerByAttribute_management command and I keep getting an error. My line of code is as simple as can be, I don't understand the problem.


    arcpy.SelectLayerByAttribute_management("landuse", "NEW_SELECTION", " [LU_2008]= 'Cemetery' ")

    Runtime error <class 'arcgisscripting.ExecuteError'>: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute).




    Any help or suggestions are much appreciated,
    mike

  4. #4
    Mike Wissner
    Join Date
    Feb 2012
    Posts
    65
    Points
    1
    Answers Provided
    0


    0

    Default Re: SelectLayerByAttribute_management - Correct syntax for expression

    Ok,

    I figured out that for my code I don't need the brackets around the field name.

    But now when I run the code, it says its completed successfully, but no selection is visible in my data frame. Very frustrating.

  5. #5
    Bruce Nielsen
    Join Date
    Jan 2011
    Posts
    299
    Points
    130
    Answers Provided
    21


    0

    Default Re: SelectLayerByAttribute_management - Correct syntax for expression

    The field name needs to be enclosed by double quotes:
    Code:
    arcpy.SelectLayerByAttribute_management("landuse", "NEW_SELECTION", "\"LU_2008\" = 'Cemetery' ")
    GIS Specialist
    USDA-Natural Resources Conservation Service

  6. #6
    Mike Wissner
    Join Date
    Feb 2012
    Posts
    65
    Points
    1
    Answers Provided
    0


    0

    Default Re: SelectLayerByAttribute_management - Correct syntax for expression

    Thanks Bruce.

    Problem solved, I wasn't escaping the double quotes around the field name.

    good luck Katrina
    Mike Wissner

    Geospatial Analyst
    LocationAge
    mike.wissner@locationage.com

  7. #7
    Katrina Bennett
    Join Date
    Mar 2011
    Posts
    5
    Points
    0
    Answers Provided
    0


    0

    Default Re: SelectLayerByAttribute_management - Correct syntax for expression

    I tried what was suggested, to remove the backslashes. I am still getting the error:

    #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)


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

  8. #8
    Bruce Nielsen
    Join Date
    Jan 2011
    Posts
    299
    Points
    130
    Answers Provided
    21


    0

    Default Re: SelectLayerByAttribute_management - Correct syntax for expression

    I wonder if this might make a difference:
    Code:
    expression1 = 'ABS("ELEV" - "Elev_metre") <= 100 '
    expression2 = 'ABS("SLP" - "mean_slp") <= "sd_slp" '
    expression3 = 'ABS("ASP" - "mean_asp") <= "sd_asp" '
    GIS Specialist
    USDA-Natural Resources Conservation Service

  9. #9
    Katrina Bennett
    Join Date
    Mar 2011
    Posts
    5
    Points
    0
    Answers Provided
    0


    0

    Default Re: SelectLayerByAttribute_management - Correct syntax for expression

    Ok, it worked with some minor modification! In the end I had to make a new column for ELEV because the original column had different precision and scale, which apparently matters in the Python version of the Select Layer By Attribution, but not the Tool version.

    Also, I combined these together using the ADD because the SUBSET_SELECTION did not appear to be working. I kept getting a zero selection in my shapefile, which was incorrect.

    Thank you for you help!

    Final code lines used are as follows:

    expression1 = 'ABS("Elev2" - "Elev_metre") <= 100 AND ABS("SLP" - "mean_slp") <= "sd_slp" AND ABS("ASP" - "mean_asp") <= "sd_asp" '
    #expression2 = 'ABS("SLP" - "mean_slp") <= 2*"sd_slp" '
    #expression3 = 'ABS("ASP" - "mean_asp") <= 2*"sd_asp" '

    arcpy.SelectLayerByAttribute_management(sel_pts_lyr, "NEW_SELECTION", expression1)

    Katrina

  10. #10
    Terrence Dolan
    Join Date
    Jan 2012
    Posts
    1
    Points
    0
    Answers Provided
    0


    0

    Default Re: SelectLayerByAttribute_management - Correct syntax for expression

    Question regarding arcpy.SelectLayerByAttribute_management:

    I can not figure out the problem with the following syntax:

    arcpy.SelectLayerByAttribute_management("Faults_2","NEW_SELECTION"," [names2]='Wabash Valley liquefaction features' ")

    Result: Runtime error <class 'arcgisscripting.ExecuteError'>: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute).



    The layer "Faults_2" is a geodatabase feature class. I have zero experience with Python, any help is much appreciated!

    -Terry

  11. #11
    Adel Itani
    Join Date
    Mar 2012
    Posts
    9
    Points
    0
    Answers Provided
    0


    0

    Default Re: SelectLayerByAttribute_management - Correct syntax for expression

    Hy Katrina,
    Just a notice:
    did you include the next line:
    Code:
    import arcpy  # or argisscripting
    # ...

  12. #12
    Bruce Nielsen
    Join Date
    Jan 2011
    Posts
    299
    Points
    130
    Answers Provided
    21


    0

    Default Re: SelectLayerByAttribute_management - Correct syntax for expression

    Terry,

    This should work for you:
    Code:
    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.
    GIS Specialist
    USDA-Natural Resources Conservation Service

  13. #13
    Tony Enberg
    Join Date
    Jul 2010
    Posts
    1
    Points
    0
    Answers Provided
    0


    0

    Default Re: SelectLayerByAttribute_management - Correct syntax for expression

    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

  14. #14
    Bruce Nielsen
    Join Date
    Jan 2011
    Posts
    299
    Points
    130
    Answers Provided
    21


    0

    Default Re: SelectLayerByAttribute_management - Correct syntax for expression

    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:
    Code:
    arcpy.SelectLayerByAttribute_management("FCname", "NEW_SELECTION", '"integerField" = %i' % variableName)
    GIS Specialist
    USDA-Natural Resources Conservation Service

+ Reply to Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts