Tuesday, 9 April 2019

arcgis 10.1 - Multivalue input from chosen field giving patchy input strings from ArcPy?


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



Answer



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.


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...