Multivalue query

1099
6
Jump to solution
03-26-2013 12:38 PM
TonyAlmeida
Occasional Contributor II
I am trying to create a script that allow me to enter more then one value to search, select and create a new feature from the selected. My script runs and finished fine but produces no out feature class. I am not sure if i am doing it correctly or not but any help would be great.

Thanks.

import arcpy  arcpy.env.overwriteOutput = True mxd = arcpy.mapping.MapDocument("CURRENT") df = arcpy.mapping.ListDataFrames(mxd, "Layers")[0] lyr = arcpy.mapping.ListLayers(mxd, "Taxparcels")[0]  #Define input/output input = arcpy.GetParameterAsText(0) fieldName = arcpy.GetParameterAsText(1) values = arcpy.GetParameterAsText(2) output = arcpy.GetParameterAsText(3)  #Build the where clause queryJoin = "' OR " + fieldName + " = '" whereClause = fieldName + " = '" + queryJoin.join(values) + "'"  #Select(input, output, whereClause) arcpy.SelectLayerByAttribute_management(lyr, "NEW_SELECTION", whereClause) if int(arcpy.GetCount_management("Taxparcels").getOutput(0)) > 0:     arcpy.Select_analysis("Taxparcels", output) 


Script parameters
are attached.
Tags (2)
0 Kudos
1 Solution

Accepted Solutions
curtvprice
MVP Esteemed Contributor
#Build the where clause whereClause = "{0} in ('{1}')".format(fieldName, "','".join(values.split(';')))


It's a little trickier than this because the each string value needs to be quoted.

#Build the where clause values = values.split(";")  # split values into list values = ["'{0}'".format(v) for v in values] # add single quotes whereClause = "{0} IN ({1})".format(fieldName, ",".join(values))  


This will generate a string that would work as a SQL expression like:

FIELD1 IN ('R12351','R12352')

UPDATE: Mathew Coyle's solution also works. I didn't see that he was joining using the string "<single-quote><comma><single-quote>", ie "','".  Neat!

He's right: Potato, tomato. I just can't resist showing off that most awesome of Python constructs, the "list comprehension", sorry.

View solution in original post

0 Kudos
6 Replies
MathewCoyle
Frequent Contributor
If you are only making selections from one field you can make a much easier query using the 'IN' command.

Instead of  this
query = 'field = x or field = y or field = z' #etc

It would be this
query = 'field in (x, y, z)'

You also don't need to run a separate selection when using the select tool, you can input the where clause directly in the tool.

The main issue you are probably having is your values variable is a string and you are trying to add it how generally lists are used. How do you plan on having your users input multiple values? You would need to post a sample of your valid field values and the actual properties of your values parameter to get more help.
0 Kudos
TonyAlmeida
Occasional Contributor II
the values would be manually entered like this (R12351, R12352). I have attached a sample of the data.
Thank you for replying.
0 Kudos
MathewCoyle
Frequent Contributor
Something like this should work. Should be all you need after your inputs.
#Build the where clause
whereClause = "{0} in ('{1}')".format(fieldName, "','".join(values.split(';')))

#Select(input, output, whereClause)

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

    arcpy.Select_analysis(lyr, output, whereClause)
0 Kudos
curtvprice
MVP Esteemed Contributor
#Build the where clause whereClause = "{0} in ('{1}')".format(fieldName, "','".join(values.split(';')))


It's a little trickier than this because the each string value needs to be quoted.

#Build the where clause values = values.split(";")  # split values into list values = ["'{0}'".format(v) for v in values] # add single quotes whereClause = "{0} IN ({1})".format(fieldName, ",".join(values))  


This will generate a string that would work as a SQL expression like:

FIELD1 IN ('R12351','R12352')

UPDATE: Mathew Coyle's solution also works. I didn't see that he was joining using the string "<single-quote><comma><single-quote>", ie "','".  Neat!

He's right: Potato, tomato. I just can't resist showing off that most awesome of Python constructs, the "list comprehension", sorry.
0 Kudos
MathewCoyle
Frequent Contributor
It's my lazy way, but it still works. Though yours is more pythonic I would wager. Potato, tomato.
0 Kudos
TonyAlmeida
Occasional Contributor II
Works great thank you to both of you!
0 Kudos