1

I made a small tool that can be run from a toolbox. It has a validation code to choose multiple values from a specified field from a specified feature layer. It looks quite ordinary:

tool

The validation code is:

def updateParameters(self):
   if self.params[1].altered:
      if self.params[1].value:
         values = set()
         inlayer = self.params[0].value.value
         fieldname = self.params[1].value.value

         cursor = arcpy.SearchCursor(inlayer, None, None, infield)
         for row in cursor:
            values.add(row.getValue(infield))

         self.params[2].filter.list = sorted(values)

It gives me a nice multivalue list of values. But when I check some of them and run a simple script:

import arcpy
# Variables
inlayer = arcpy.GetParameterAsText(0)
infield = arcpy.GetParameterAsText(1)
invalues = arcpy.GetParameterAsText(2)
arcpy.SetParameter(3, invalues)

selected_values = invalues.split(";")

for selected_value in selected_values:
    arcpy.AddWarning(selected_value)

then it returns some unicode strings with an apostrophe and some not. I found out that if there is a gap in the value, it returns an unicode string with an apostrophe. I don´t get it why? There´s a example output of the forementioned script:

enter image description here

In this tool window I can see that these apostrophes already figure as input values!

Why is that, and how do I avoid it?

These inputs with apostrophes troubles me further in my script because

if "withoutgap":
  # returns True but
if "with gap":
  # returns False because the unicode string has apostrophes

running on Windows 8.1, ArcGIS for Desktop 10.1 Basic, Python 2.7

1
  • The main difference between the first two and the last two strings I can see is that the first two contain spaces. This is a wild guess but possibly the validator automatically wraps values with spaces in quotes?
    – Menno
    Oct 23, 2014 at 10:02

1 Answer 1

2

I'm not a python expert so your code looks OK to me, I assume you are using a set() as a way of removing duplicates? If not and your dataset really does have only 4 rows in it then try using a list as all the examples in the ESRI help file uses lists...

Before you try that I would spoof up a second dataset which YOU have created and typed in those words that you want in the list. If that works as expected then it would suggest its a problem in the source dataset. Now this would not surprise me if the dataset has originated from say EXCEL which allows users to type and copy anything into it... So visually everything looks the same but behind the scenes it could have non-visible characters.

3
  • Yep, I´m using set() to remove duplicates. Each of these 4 values has many rows in a table so using a list is unfortunately not an option. The dataset I used for this example is created by me and those values are typed in - I wanted a clean dataset with "normal" values so I had typed them in manually. I know that spaces in these values force the validator to make a different output with apostrophes but I still don´t know why...
    – jonlew
    Oct 23, 2014 at 11:42
  • 1
    Hmm, OK you use GetParameterAsText() to create invalues, I wonder if you could try using GetParameter() and manipulate the returned object (I guess it will be a list object?). Just an idea?
    – Hornbydd
    Oct 23, 2014 at 15:59
  • Yes it works like a charm! I have always used GetParameterAsText() somehow automatically and GetParameter() never came to my mind. Great!
    – jonlew
    Oct 28, 2014 at 17:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.