Select to view content in your preferred language

Multivalue Parameter in Custom tool adds extra quotes

957
3
07-19-2012 07:20 AM
ThomasStanley-Jones
Deactivated User
I struggled with this tool for many hours before I realized what was going wrong and why.  The issue was with a space in the name of the Feature layers that were being chosen in a multivalue parameter.  So this string would be returned: "'blah blah\blah';'meh meh\meh'".  Then when I tried to split out the string with .split(";"), the extra quotes would stay with the string and the tool that this value was passed to would break.  When there wasn't a space in the chosen parameters, I would get something like this: "blah;meh" and that would work fine.

Do I need to make this check in all my code when I don't know what the users will choose?  I haven't seen this in any of the examples in the help, so it caught me by surprise.  How do other people deal with this?

Here is the code that I came up with that works.

import os
import arcpy as ap

lines = ap.GetParameterAsText(0).split(";")
claims = ap.GetParameterAsText(1).replace("'","")
claims = claims.split(";")

for line in lines:
    for claim in claims:
        output = os.path.join(ap.GetParameterAsText(2),line + "_" + claim.split("\\")[1])
        ap.Intersect_analysis([line, claim],output,"","","LINE")
3 Replies
markdenil
Frequent Contributor
That is pretty much what I would do; strip the unwanted characters with replace.
Do you need to make this check in all your code when you don't know what the users will choose?
You betcha.
A lot of problems can be traced to a loose nut on the keyboard.
0 Kudos
ThomasStanley-Jones
Deactivated User
For those that see this still, I've refined the code a bit from above.  Now if I'm getting values from a Multiple Parameter list in a custom tool I use this to get a clean list:

>> cleanList = [param.replace("'","") for param in arcpy.GetParameterAsText(0).split(";")]

I've never found any documentation on this from ESRI and it took a lot of time and frustration for me to figure this out as I wasn't a programmer before I started using ArcGIS.
0 Kudos
quillaja
Occasional Contributor

Nice to see this is still irritating people literally 10 years later.

I just encountered this same problem in my tool. Having a space in the string makes the param wrap with quotes. Putting a single quote (') in the string makes the param wrap with double quotes ("). It'll escape the quotes if you put both styles in.

"P20\'30\""   <-- supposed to be P20'30"

I have been a programmer since before using ArcGIS, and this behavior of the multivalue parameter is stupid.

Anyway, using strip() is safer than replace(), since you might have quotes in the middle of your parameter choices. replace() will eliminate them all, while strip() will remove characters from both ends of the string until it encounters a character that isn't in the string passed to strip().

0 Kudos