AnsweredAssumed Answered

Select by attributes based on multivalue parameter

Question asked by lshakes on Nov 27, 2017
Latest reply on Nov 29, 2017 by xander_bakker

New to Python.  I'm attempting to create a python script that will allow a user to select a parcel (a parameter input by user), select all other parcels within a specified distance of the selected parcel, and then export the selection.  I've gotten the code to work when the user inputs one parcel id.  However, there are times when the user will need to select multiple parcels.  I've changed the parameter to multivalue so that multiple inputs are accepted but the code needs some help to be able to handle the multiple inputs.  Here's what I have:


# Import system modules
import arcpy
import arcpy.mapping

mxd = arcpy.mapping.MapDocument ('CURRENT')
df = arcpy.mapping.ListDataFrames(mxd,"Layers")[0]
lyr = arcpy.mapping.ListLayers(mxd, "Parcels")[0]


taxid = arcpy.GetParameterAsText(0)


# Overwrite pre-existing files
arcpy.env.overwriteOutput = True


# Selects the parcel of interest
arcpy.SelectLayerByAttribute_management("Parcels", "NEW_SELECTION", '"TAX_IDTXT" = ' + "'" + taxid + "'")




Obviously this is only a small portion of the code but this is the part that I'm struggling with.  How do I get the code to accept multiple inputs from the user?  From other forum posts people have suggested a whereClause.  I've experimented with some code below without success.  I think an issue is with the TAX_IDTXT field.  I'm not sure how to tell it to search the TAX_IDTXT field in the Parcels layer.  Any ideas?


#Build the where clause
whereClause = "{0} in ('TAX_IDTXT')".format("TAX_IDTXT", "','".join(taxid.split(';')))

#Select(input, output, whereClause)

if int(arcpy.GetCount_management(lyr).getOutput(0)) > 0:

arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", whereClause)