Select to view content in your preferred language

Python toolbox multivalue parameter - how to handle embedded quotes

3691
9
01-27-2021 04:14 PM
DonMorrison1
Frequent Contributor

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.

DonMorrison1_0-1611792471976.png

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.

0 Kudos
9 Replies
EmmaHatcher
Frequent Contributor

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!

0 Kudos
DonMorrison1
Frequent Contributor

That's a great idea - unfortunately it didn't seem to work quite right for me.  Here is what I get:

DonMorrison1_0-1611794917033.png

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"]

 

0 Kudos
EmmaHatcher
Frequent Contributor

Well darn, that has worked for me with SQL queries in Python where I have apostrophes in the table values... But it looks like @DanPatterson may have the solution with using "don\'s"!

0 Kudos
DanPatterson
MVP Esteemed Contributor

backslash literals as well

 for i in ["a", "b", "don\'s c", "d"]:
    print(i)
    
a
b
don's c
d

  


... sort of retired...
0 Kudos
DanPatterson
MVP Esteemed Contributor

Defining parameters in a Python toolbox—ArcGIS Pro | Documentation

of course it might be useful if the examples on the help topic showed some corner cases or gotcha's 😉


... sort of retired...
0 Kudos
DonMorrison1
Frequent Contributor

All good ideas, but still no success.  

DonMorrison1_0-1611798707792.png

 

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.

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

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.

Good luck

z = ", ".join(["a", "b", "don's c", "d"])
z
"a, b, don's c, d"

z.split(", ")
['a', 'b', "don's c", 'd']

... sort of retired...
0 Kudos
DonMorrison1
Frequent Contributor

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"]

 

 

0 Kudos
DanPatterson
MVP Esteemed Contributor

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 )


... sort of retired...
0 Kudos