Script tool multiple input fields parameter...HELP!

5320
5
Jump to solution
03-03-2015 12:45 PM
MatthewRusso
New Contributor II

I have a script tool where it takes an input table and "MULTIPLE INPUT FIELDS"

It then runs a cursor and all i want to do is print out each row with the 3 field values.  If i do 3 parameters for the fields, it works.  If i do the mutliple it wont.  I tried printing out the value and it looked like this:  fields = "ID;Lat:Long"

I guess it needs to be in list format to run through the cursor as a row?

Here is my code

inFeat = arcpy.GetParameterAsText(0)
fields = arcpy.GetParameterAsText(1)

with arcpy.da.SearchCursor(inFeat, [fields]) as cursor:
     for row in cursor:
     arcpy.AddMessage(row[0], row[1], row[2])


0 Kudos
1 Solution

Accepted Solutions
XanderBakker
Esri Esteemed Contributor

The list of fields depends on the way you read your parameter.

If you use "arcpy.GetParameterAsTekst()", then the list will be a single string where the different items are separated by semicolons. To avoid this you could use "arcpy.GetParameter()", this will yield a list of field objects. Neither one of them is directly useful for specifying the fields in a cursor.

# use:
fldstekst = arcpy.GetParameterAsText(1)
flds = fldstekst.split(';')
# or:
lst_flds = arcpy.GetParameter(2)
flds = [fld.name for fld in lst_flds]

# then use:
with arcpy.da.SearchCursor(inFeat, flds) as cursor:
    for row in cursor:
        arcpy.AddMessage(row[0], row[1], row[2])

Be aware that if less than 3 fields have been selected that this will yield an error (index out of range).

View solution in original post

5 Replies
DarrenWiens2
MVP Honored Contributor

So, this code works, or not? It shouldn't, as your message is not indented within the for loop...

Can you also clarify what "it doesn't work" means? Is there an error? If so, what is it?

Finally, can you check your output: fields = "ID;Lat:Long" Are you sure these aren't all semicolons(;)?

0 Kudos
MatthewRusso
New Contributor II

On my Testing script it looks like this

fields = arcpy.GetParameterAsText(1# Multiple Input Script Tool

if i do arcpy.AddMessage(fields) the output is: FID;Latitude;Longitude # The Three selected fields

if I try to take my "fields" parameter and insert it into my cursor, it pops an error on the for loop line: RuntimeError: A column was specified that does not exist.

with arcpy.da.SearchCursor(inFeat, [fields]) as cursor:
     for row in cursor:
     arcpy.AddMessage("{0} | Phase: {1} | OH/UG: {2}".format(row[0], row[1], row[2]))

This is what im working with

Hopefully that is better information for you

0 Kudos
DarrenWiens2
MVP Honored Contributor

Maybe it's just a copy/paste error, but your message call is not within the loop. It should be more like:

with arcpy.da.SearchCursor(inFeat, [fields]) as cursor: 
    for row in cursor: 
        arcpy.AddMessage("{0} | Phase: {1} | OH/UG: {2}".format(row[0], row[1], row[2]))

I suspect your larger problem is that fields is just text, not a list. You will likely have to parse the semicolon-delimited string into an iterable list before referencing it in the cursor.

0 Kudos
XanderBakker
Esri Esteemed Contributor

The list of fields depends on the way you read your parameter.

If you use "arcpy.GetParameterAsTekst()", then the list will be a single string where the different items are separated by semicolons. To avoid this you could use "arcpy.GetParameter()", this will yield a list of field objects. Neither one of them is directly useful for specifying the fields in a cursor.

# use:
fldstekst = arcpy.GetParameterAsText(1)
flds = fldstekst.split(';')
# or:
lst_flds = arcpy.GetParameter(2)
flds = [fld.name for fld in lst_flds]

# then use:
with arcpy.da.SearchCursor(inFeat, flds) as cursor:
    for row in cursor:
        arcpy.AddMessage(row[0], row[1], row[2])

Be aware that if less than 3 fields have been selected that this will yield an error (index out of range).

MatthewRusso
New Contributor II

The split did the trick, i tried that earlier but i believe that i put a ',' not a ';' in.

Thanks for the help

0 Kudos