I am aware that problems like mine exist (example) but I think, this is slightly different.
I want to use MultiValue to let the user choose various inputs at the same time. The problem is, that it's only possible to use MultiValue in combination with String and not, how I would need it, as SQL.
Because I'am trying to create a toolbox, these are my properties (and on the right side my MultiValue-Field in the Toolbox:
I did it like this:
multival = arcpy.GetParameterAsText(1)
vals = string.split(multival, ";")
where_clause = string.join(vals, "+")
My problem is, that my output now has quotation marks and is not combined to one big SQL-expression.
The expression I am aiming for would look like this:
"Name = 'Store_1' OR Name = 'Store_2' OR Name = 'Store_5' OR Name = 'Store_8'"
But right now it looks like this:
'Name = 'Store_2''+'OR Name = 'Store_3''+'OR Name = 'Store_4''+'OR Name = 'Store_5''
Is there a way to have the outputs without quotations marks and added to one expression?
-
1Would it also be possbile to choose values with: "Name" in ('Store_1','Store_2', etc.) ?GISbert– GISbert2017年02月20日 14:14:31 +00:00Commented Feb 20, 2017 at 14:14
1 Answer 1
Your comment on using the IN
is a good way to go; makes the code much simpler.
multival = arcpy.GetParameterAsText(1) # 'Store_1;Store_2;Store_3'
vals = multival.split(";")
where_stub = """"NAME" IN ({})"""
stores = ','.join(["'{}'".format(x) for x in vals])
where = where_stub.format(stores)
You can combine some of those lines, if you'd like.
EDIT: I just noticed your screenshot has the "OR NAME =" in there. There's no need. Just have the list of values. For example:
- Store_1
- Store_2
- etc
-
Thank you very much! Your code works finde, my output is
"NAME" IN ('Store_1','Store_2','Store_3')
. But now when I run theSelect
-Tool (arcpy.Select_analysis(Input, Output, where) the shapefile I get is empty. Any Ideas?GISbert– GISbert2017年02月21日 08:45:10 +00:00Commented Feb 21, 2017 at 8:45 -
It is probably best to ask a new question with your code and data spelled out.Richard Morgan– Richard Morgan2017年02月21日 09:32:57 +00:00Commented Feb 21, 2017 at 9:32
-
Yes, you are right. I solved it now using a for-loop. Do you think I should post it as a comment?GISbert– GISbert2017年02月21日 10:07:49 +00:00Commented Feb 21, 2017 at 10:07
-
Comments are usually for short ideas. You can also update your original question to show a solution, etc.Richard Morgan– Richard Morgan2017年02月21日 13:38:59 +00:00Commented Feb 21, 2017 at 13:38
-
Me again, your solution worked totally fine, I just messed something up in the toolbox properties. Tanks again!GISbert– GISbert2017年02月21日 19:32:04 +00:00Commented Feb 21, 2017 at 19:32