Select to view content in your preferred language

Python Tool - User input then Select by Attribute

5226
21
02-06-2017 01:22 PM
DevinUnderwood2
Regular Contributor

Any ideas why I am having problems by trying to have user input a string value for a feature, then have the feature selected. 

#Import Modules
import os,arcpy
mxd = arcpy.mapping.MapDocument ('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
#os.chdir(r'C:\Users\dunderwood\Documents\MY_TEMPLATES\WASTEWATER_EDIT_TEMPLATE')
# Set the parameters
InputFeatureClass = arcpy.GetParameterAsText(0)
InputField = arcpy.GetParameterAsText(1)
InputValue = arcpy.GetParameterAsText(2)

whereClause = FACILITYID == InputValue"
arcpy.SelectLayerByAttribute_management(InputFeatureClass, "NEW_SELECTION", whereClause)
df.zoomToSelectedFeatures()
arcpy.RefreshActiveView()

21 Replies
DarrenWiens2
MVP Honored Contributor

I don't know. The following example works for me. Try hardcoding your parameters to something you're confident should work. Also, try manually running the SelectLayerByAttribute tool through the GUI, and looking at the syntax in the Results window to see the Python syntax it generates (right-click -> Copy as Python Snippet).

InputFeatureClass = 'points'
InputField = 'bearing'
InputValue = '30'
whereclause = """{} = {}""".format(arcpy.AddFieldDelimiters(InputFeatureClass,InputField),InputValue)
arcpy.SelectLayerByAttribute_management(InputFeatureClass,"NEW_SELECTION",whereclause)‍‍‍‍‍
0 Kudos
DevinUnderwood2
Regular Contributor

Line 4 was missing a quotation. Should be like the following.

whereclause = """{} = '{}'""".format(arcpy.AddFieldDelimiters(InputFeatureClass,InputField),InputValue)

It works now, thanks.

I need the last part of the tool to work which I have the following python, so I can have a Boolean tool parameter to edit.

Familiar with arcpy editing ?

# Set Editing
workspace = r'C:\xyz'
edit = arcpy.da.Editor(workspace)  
edit.startEditing(False, True)  
edit.startOperation()
edit = arcpy.GetParameterAsText(3)
0 Kudos