0

I am new in Python. I was trying to use a variable for selecting a feature instead of giving a direct input. My code sample will explain it better:

import arcpy
from arcpy import env
env.overwriteOutput = True
env.workspace = "H:/shp"

#variables
parcel = "parcel.shp"
parcel_Layer = "parcel_Layer"

Make Feature Layer
arcpy.MakeFeatureLayer_management(parcel, parcel_Layer, "", "", "FID FID VISIBLE NONE;Shape Shape VISIBLE NONE;OBJECTID OBJECTID VISIBLE NONE;ACCOUNTID ACCOUNTID VISIBLE NONE;PropID PropID VISIBLE NONE")

parcel_Layer_selattr = arcpy.SelectLayerByAttribute_management(parcel_Layer, "NEW_SELECTION", "\"PropID\" = 142855")

This code works fine as long as I enter the property ID directly ("\"PropID\" = 142855"). But when I declare the PropID as a variable, it doesnot work. Here is the code sample:

import arcpy
from arcpy import env
env.overwriteOutput = True
env.workspace = "H:/shp"

#variables
parcel = "parcel.shp"
parcel_Layer = "parcel_Layer"
x = 142855
Make Feature Layer
arcpy.MakeFeatureLayer_management(parcel, parcel_Layer, "", "", "FID FID VISIBLE NONE;Shape Shape VISIBLE NONE;OBJECTID OBJECTID VISIBLE NONE;ACCOUNTID ACCOUNTID VISIBLE NONE;PropID PropID VISIBLE NONE")

parcel_Layer_selattr = arcpy.SelectLayerByAttribute_management(parcel_Layer, "NEW_SELECTION", "\"PropID\" = x")

This is not working. it gives me this error msg:

 Runtime error  Traceback (most recent call last):   File "<string>", line 1, in <module>   File "c:\program files (x86)\arcgis\desktop10.1\arcpy\arcpy\management.py", line 6435, in SelectLayerByAttribute     raise e ExecuteError: ERROR 000358: Invalid expression Failed to execute (SelectLayerByAttribute).

What I am doing wrong?

0

problem solved. Found Jason's (thanks to him) post in another thread and here is the working code:

parcel_Layer__selattr = arcpy.SelectLayerByAttribute_management(parcel_Layer, "NEW_SELECTION", "\"PropID\" = {}".format(x))
| improve this answer | |

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