2

Is it possible to use the contents of a single column csv file to limit the list of acceptable inputs for a tool in a python toolbox? I'm aware that I can use the code below to achieve my requirements, but the list I need often varies outside of ArcGIS and outside of my control and therefore the code would need regular updates.

options1 = [1, 2] param0.filter.type = "Value List" param0.filter.list = options1

2

Try opening the csv and loading the values into a list.

import csv

param0.filter.type = "Value List"
with open('c:/pythontools/file.csv', 'rb') as f:
    reader = csv.reader(f)
    param0.filter.list = [item for sublist in list(reader) for item in sublist]

Edit: By default, the list(reader) returns a list, so we need to flatten the list of lists into a single list.

You will also want to fully specify the path of the csv file to avoid any path issues.

This post on StackOverflow has more details.

8
  • Thanks for your help Richard. Using the code above isn't working for me. I'm ending up with a little red cross on the tool's icon. Trying to run it produces an error box saying "Unable to execute the selected tool.". – davehughes87 Sep 30 '16 at 12:12
  • I should point out that if I manually specify the options list, the tool works correctly, so the issue is being caused by the csv code. – davehughes87 Sep 30 '16 at 12:31
  • @davehughes87, can you give the error message in full? – fatih_dur Sep 30 '16 at 12:44
  • @davehughes87 I forgot the list(reader) returns a list even if there's just a single entry. As the filter.list needs a flattened list, we need to sample out the values. See updated code. – Richard Morgan Sep 30 '16 at 12:47
  • The tool runs fine without using the csv code. Adding the csv code means the tool is not placed in the correct folder and instead shows up with a red cross bit.ly/2cQvvKD. ArcMap suggests the syntax of the toolbox is correct and will still run all other tools. Attempting to run this tool results in the following window. bit.ly/2dpZBpi – davehughes87 Sep 30 '16 at 12:48

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.