I'm trying to have a user input a county in my tool, and then use that string to select from a layer of counties.
County = arcpy.GetParameterAsText(0)
#Select county to clip
arcpy.MakeFeatureLayer_management ("W:/Homework117/Final Project -- B&B/Data/FinalProject.gdb/WestCoastCounties", "countieslyr")
arcpy.SelectLayerByAttribute_management ("countieslyr", "NEW_SELECTION", '[NAME] = County')
x = arcpy.CopyFeatures_management("countieslyr", "CountyOfChoice")
The SelectLayerByAttribute process produces the following error:
Traceback (most recent call last):
File "W:\Homework117\Final Project -- B&B\PythonCode\FirstCode.py", line 29, in <module>
arcpy.SelectLayerByAttribute_management ("countieslyr", "NEW_SELECTION", '[NAME] = County')
File "C:\Program Files (x86)\ArcGIS\Desktop10.3\ArcPy\arcpy\management.py", line 7221, in SelectLayerByAttribute
raise e
ExecuteError: ERROR 000358: Invalid expression
Failed to execute (SelectLayerByAttribute).
Countyis not surrounded by any quotes, and it needs to be since it is a string. I don't think the parameter returns the value with quotes. It simply returns the value. When you build your expression, you need to have quotes added to it. It may be easier to do this by modifying the contents of the variable. – Get Spatial Mar 22 '17 at 19:09