This question already has an answer here:

All I'm trying to do is use SelectLayerByAttribute to select all lines less than 5.1 meters. However, no matter what query I try I keep getting "error 000358 invalid expression" when I run the script as a tool. I have already tried using various combinations of escape characters and string concatenations like ' \"Shape_Length\" < \'5.1\' ' and '\"Shape_Length\" <' + '5.1'. I've also have tried putting Shape_Length in {}'s. I tried doing the same query in the python window in ArcMap and all I have to do is "Shape_Length < 5.1" and it works just fine. I've never been so stumped. Anyone have anymore ideas on what to try?

import arcpy
import os
from arcpy import env
from arcpy.sa import *
from arcpy import da

arcpy.env.overwriteOutput = True

og_polyline = arcpy.GetParameterAsText(0)
polyline =arcpy.CopyFeatures_management(og_polyline,r"in_memory/lines")




query="Shape_Length < 5.1"

arcpy.SelectLayerByAttribute_management(layer, "NEW_SELECTION", query)

marked as duplicate by PolyGeo arcpy Apr 8 '18 at 22:38

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • query="Shape_Length < + 5.1" should be query="Shape_Length < 5.1". When querying the field delimiters aren't strictly needed and because the shape_length field is a double the value should not be quoted. – Michael Stimson Apr 25 '17 at 21:30
  • My apologies that + sign was a typo I accidentally left in the code. I already tried "Shape_Length < 5.1" and the error is still happening. – Rdubya54 Apr 25 '17 at 21:36
  • Does that field exist? You're copying to an in_memory workspace but from what? If it's a shapefile then the field doesn't necessarily exist. Note that polyline should be r"in_memory/lines", at the moment it's a gpResult object. – Michael Stimson Apr 25 '17 at 21:38
  • 1
    Shape_Length does not exist in memory. I'll try to find a duplicate that says more. – PolyGeo Apr 25 '17 at 21:39