I have the following Select Layer By Attribute statement throwing a 000358 invalid expression error using Python with ArcGIS 10.4. The input feature layer is valid because I can select features using a select by location. When I try to remove some features from the selection the script fails. There is an attribute in the feature dataset feature class called returned which stores data with a data type short. Why is this line of code giving an error?

arcpy.SelectLayerByAttribute_management("inFC_lyr2", "REMOVE_FROM_SELECTION", "\"returned\" = 1")

Per the suggestions below I added these lines to the code and got the same error.

inFC2 = r"Z:\Data\Planning\Buildings\Housing.gdb\AdressesStructures\Households"
sql_exp = """{0} = {1}""".format(arcpy.AddFieldDelimiters(inFC2, "returned"), 1)
print sql_exp

Interestingly sql_exp printed returned = 1 to the screen. Shouldn't that be "returned" = 1?

  • Why the backslashes? Try using Add Field Delimiters: desktop.arcgis.com/en/arcmap/10.3/analyze/arcpy-functions/… – BERA Apr 25 '17 at 18:14
  • Because the query needs to have the quotes as delimiters because it is in a file geodatabase. The backslashes guarantee the quotes. That should pass "returned" = 1 to the tool. That is how I have always done it. – GBG Apr 25 '17 at 18:21
  • 1
    Ok! My suggestion is to instead use Add Field Delimiters – BERA Apr 25 '17 at 18:24
  • What type of GDB are you using? Check field delimiters. – Barbarossa Apr 25 '17 at 18:28
  • 1
    Understood, it is just that the use of the field delimiter guarantees the correct delimiter for the data type. In my case I have a file geodatabase and the delimiter is a double quote. – GBG Apr 25 '17 at 18:33

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.