2

I have a python toolbox I built with a tool which takes multiple excel files as an input parameter. The problem is I cannot figure out how to get the files or file names in order to import them in the execute block.

relevant code:

    def getParameterInfo(self):
    param0 = arcpy.Parameter(
        displayName = "Service Tap Placement output excels",
        name = "Excels",
        datatype = "DEFile",
        parameterType = "Required",
        direction = "Input",
        multiValue = True)
    param0.filter.list = ['xls', 'xlsx']

and

    def execute(self, parameters, messages):

    # from parameters
    excelList = parameters[0].values
    move = parameters[1].value
    myPath = parameters[2].valueAsText

things I've tried :

1) defining excelList as shown in the second code block, returns: [geoprocessing value object object at 0x28BFC848, geoprocessing value object object at 0x28BFC770, geoprocessing value object object at 0x28BFC3C8]

2) excelList = parameters[0].valueAsText, returns: C:\Workspace\TapResnap\testE_script\Results_ServiceTapPlacement\Results_nfp01.xls;C:\Workspace\TapResnap\testE_script\Results_ServiceTapPlacement\Results_nfp02.xls;C:\Workspace\TapResnap\testE_script\Results_ServiceTapPlacement\Results_nfp03.xls

Problem is I can't figure out how to access the objects (from try 1) as needed, I got close using Multivalue parameter as text in Python toolbox - reading in to imported module function but I'd like to avoid having to separate them out from one string (try 2) if possible (there must be an easier way), and obviously I'm not working with rasters (is there something similar for my case?). I also see some promise here, but I'm not fully understanding how to apply it to my situation.

3) adding "return excelList" to the execute block (just to be able to play with it in the python window, I'm actually trying to import them to arc tables) after using the same assignment as try 1, importing the toolbox, then setting a variable to the tool result in the python window: fileList = arcpy.[Tool]_Toolbox... but then I have the same problem as try 1, I'm unsure how to access the individual file values (in any way).

This is my first python toolbox experience, and I have (obviously) limited experience dealing with result objects and output parameters.

  • In your module, def execute(self, parameters, messages), where does your "parameters" variable come from? I don't see any mention of it in the preceding module, only a "param0" variable. – Waterman Jun 6 '18 at 0:48
3

You're on the right track. From the value object returned by .values, you need to extract the actual value. There's a few ways that can be done:

excelList = [v.value for v in parameters[0].values] # returns unicode
excelList = map(str, parameters[0].values)

Or, (this is more useful for multi-column data):

vt = arcpy.ValueTable()
vt.loadFromString(parameters[0].valueAsText)
excelList = [vt.getTrueValue(i, 0) for i in xrange(vt.rowCount)] # returns unicode
  • Aha! v.value is exactly what I needed for this, thank you!The rest will all be very useful too. – maverickGIS Dec 19 '16 at 16:11
2

For sake of completeness, an alternative is:

#Split on  semicolon and strip of any single quotes
#that ArcGIS for some reason wraps around filepaths with spaces
excelList = [xl.strip("'") for xl in parameters[0].valueAsText.split(";")]
  • 1
    Note, this won't work if your paths have semicolons in them. And that's one of the reasons to wrap in single quotes :) It's likely for the same reason that a CSV will wrap values in quotes. (Granted, having semicolons in paths is strange). – Paul Dec 18 '16 at 19:56
  • Very strange ;) but apparently legal, even though a semicolon is the Windows PATH separator. – user2856 Dec 19 '16 at 19:44
  • 2
    I just tested and ArcGIS doesn't add single quotes around paths with semicolons in a multivalue parameter, just paths with spaces. – user2856 Dec 19 '16 at 22:11
  • that's a bit scary. Good to know though! – Paul Dec 19 '16 at 22:43
  • @Waterman you should comment on maverickGIS's question, not on my answer. – user2856 Jun 6 '18 at 0:35

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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