I have a multivalue parameter in my python tool and the values may include characters such as quotes. When I select such a value from the list (and the one after it) and click run I get an error below.
Since this all happens even before any of my toolbox functions are called (in any case they are empty) there must be some trick in specifying the list of value in the first place to get this to work. Here is my toolbox source that shows defining the parameter. These are the only modifications I made to the default tool code.
def getParameterInfo(self): """Define parameter definitions""" p1 = arcpy.Parameter( displayName='Pick list', name='p1', datatype='GPString', parameterType='Optional', direction="Input", multiValue=True) p1.filter.type = "ValueList" p1.filter.list = ["a", "b", "don's c", "d"] params = [p1] return params
In reality the list is populated from values looked up in a database so I don't have any control whether they contain quotes and it isn't an option to not include strings with quotes.
I'm running on the python that comes with ArcPro 2.6.1 - Windows.
This will work if you replace single apostrophes with double apostrophes (and I mean double apostrophes, not quotation marks) in your python code. So your variable would be p1.filter.list = ["a", "b", "don''s c", "d"]. I know, it's kind of weird, but so it is.
Best of luck!
That's a great idea - unfortunately it didn't seem to work quite right for me. Here is what I get:
I think I got the apostrophes right - and I even tried copying directly from your post.....
p1.filter.list = ["a", "b", "don''s c", "d"]
All good ideas, but still no success.
p1.filter.list = ["a", "b", "don\'s c", "d"]
There really should be no reason to have to escape a quote in a literal (assuming you use a double quotes to surround it). In my case I'm not even using literals - I pull the values out of the database. My guess is that at some point they (ESRI) flatten the list to a single string which is where they into trouble - I have no idea why they would want to do something like that but it would be nice to hear from the ESRI team on this.
You will have to file a case with Tech Support to get any attention.
To add to the mystery, it must be something non-python, since it handles joining and splitting as designed.
z = ", ".join(["a", "b", "don's c", "d"]) z "a, b, don's c, d" z.split(", ") ['a', 'b', "don's c", 'd']
I decide to just work around this be creating a list of "sanitized" items for my multivalue parameter - basically replacing double quotes, single quotes, and semicolons with a harmless back tick (`) character. Of course I have to keep track of the mapping between the alias and the real value and hope the mapping remains one to one so I can make sense of the selections. For instance the list below would be a problem.....
["a", "b", "don's c", "don;s c", "d"]
It would be converted to
["a", "b", "don`s c", "don`s c", "d"]
on a side note... I couldn't replicate the behaviour in conventional python toolboxes where you fill in the value list within the dialog. It even worked when filling the values in the parameter arguments section rather than the dialog option. Something must get lost in the translation from/to the pyt variant. It might be worth flagging Tech Support since the issues may run deeper and a new toolbox structure for users appears to be on the horizon (aka, the system tbx files now have a folder structure check out C:\Your_Install_Path\Resources\ArcToolBox\toolboxes )