I'm developing an ArcGIS tool by Python. Below is a part of the inputs:

StudyArea = arcpy.GetParameterAsText(0)

LandCover = arcpy.GetParameterAsText(1)

gridcode = arcpy.GetParameterAsText(2)

gridcode is a field from LandCover.

gridcode aims to derive the field which contains land cover class code.

Then I try to select some land cover types (land cover code < 17 and = 21), and the code I write is:

arcpy.SelectLayerByAttribute_management("LC_test","NEW_SELECTION",'gridcode<17 or gridcode=21')

"LC_test" is the feature layer of LandCover

The problem is: in the LandCover shapefile, if the field of Land Cover code is named as "gridcode" as well, the tool works fine.

If the field name is not "gridcode", the tool fails to execute. The error is "invalid expression".

Can anyone help me with this?


You're using gridcode as text in a string, instead of a variable.


arcpy.SelectLayerByAttribute_management("LC_test","NEW_SELECTION", '"' gridcode + '" < 17 or "' + gridcode + '" = 21')

The exact syntax you're after depends on data sources and field types. See the ArcGIS SQL reference for query expressions used in ArcGIS help.

  • Thanks you,however, it still shows "invalid expression" – loshaneve Aug 29 '15 at 13:23
  • I've updated my answer. – Emil Brundage Aug 29 '15 at 13:55

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.