0

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).
  • 1
    It looks like the text for County is 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
  • Welcome to GIS SE! As a new user please take the tour to learn about our focused Q&A format. – Midavalo Mar 22 '17 at 20:56
0

You need to get the value of your County parameter inside your query string

County = arcpy.GetParameterAsText(0)

arcpy.MakeFeatureLayer_management ("W:/Homework117/Final Project -- B&B/Data/FinalProject.gdb/WestCoastCounties", "countieslyr")

query = """ [NAME] = '{0}' """.format(County)

arcpy.SelectLayerByAttribute_management ("countieslyr", "NEW_SELECTION", query)
x = arcpy.CopyFeatures_management("countieslyr", "CountyOfChoice")

You could also try letting arcpy determine the correct way to refer to fields:

query = """ {0} = '{1}' """.format(arcpy.AddFieldDelimiters("countieslyr", 'Name'),  County)
  • Still has the same error. In my Parameter, I input the name of the county manually as a string, does that have any impact? – Sam Kennard Mar 22 '17 at 20:12
  • @SamKennard I have updated my answer with a slight modification. Make sure your query is below the MakeFeatureLayer() and use the new query line I've added at the bottom – Midavalo Mar 22 '17 at 20:46
  • @SamKennard Good to hear! – Midavalo Mar 22 '17 at 20:56
0

As Get Spatial mentioned you need to insert the variable into your code. Try something like the following:

where = '[NAME] = ' + "'%s'" % County
arcpy.SelectLayerByAttribute_management ("countieslyr", "NEW_SELECTION", where)

This is assuming that your field delimiters are correct. For an explanation and help on that topic, see here:

Including variable in where clause of arcpy.Select_analysis()?

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.