1

I'm trying to create a drop down menu in a Python script in ArcMap where one input is a CSV and the other input is a drop down of the headers in that CSV. Is this possible at all?

enter image description here

In the image above, "Candidate List" is the supplied CSV and "Candidate Field" is the field header that the user selects for the column that the data is located in. Would I have to put something in the script its self or in the update parameters?

with open(Candidate List, "rb") as f:
 reader = csv.reader()
 i = reader.next()

Wherein I want I to be the fields for that drop down.

PolyGeo
65.5k29 gold badges115 silver badges349 bronze badges
asked Jul 27, 2017 at 13:50

2 Answers 2

3

It is definitely possible. You would need to use script tool validation class.

  1. Read the .csv headers (can be done with csv or pandas, for instance).

With csv:

import csv
csv.DictReader(open(r"C:\GIS\grafiti.csv","r")).fieldnames
['FID', 'OBJECTID', 'CID', 'SqFtAprox', 'POINT_X', 'POINT_Y']

With pandas:

import pandas as pd
pd.read_csv(r"C:\GIS\grafiti.csv").columns.tolist()
['FID', 'OBJECTID', 'CID', 'SqFtAprox', 'POINT_X', 'POINT_Y']
  1. Use updateParameters to set values of the needed parameter to the list of columns.

Code:

self.params[2].filter.list = headers

Please look up the help page for more examples. Another helpful answer is here.

answered Jul 27, 2017 at 18:25
2
  • The only issue is that the CSV being input will always be different for different users, can this still be implemented? Commented Jul 27, 2017 at 20:43
  • Of course. You will be updating your .csv headers parameter list values every time a user browses to her .csv file. The list values will not be stored within the tool itself; it will be read dynamically upon every tool execution after users specifies the input .csv file. Commented Jul 28, 2017 at 3:55
0

Found the solution, see below:

def updateParameters(self):
 if self.params[4].value and \
 (self.params[4].value.value.endswith("xls") or self.params[4].value.value.endswith("xlsx")):
 import xlrd
 book = xlrd.open_workbook(self.params[4].value.value)
 sheet = book.sheet_by_index(0)
 headers = [str(cell.value) for cell in sheet.row(0)]
 self.params[5].filter.list = headers
answered Jul 27, 2017 at 21:23
2
  • Hm, isn't it what I have told you to do? ;) in the question, you talk about .csv, now you are reading .xls. But glad you got something working anyways :) Commented Jul 28, 2017 at 3:53
  • It is, yes. I have it configured for both xls and csv depending on user input. I wasn't quite sure how to implement the self.params[4] and I wasn't aware that I had to use self.params[4].value.value in order to actually open the file. Commented Jul 28, 2017 at 14:46

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.