Select to view content in your preferred language

arcpy.GetParameterAsText to get a list of numbers

3472
4
06-10-2011 12:04 PM
WeiLuo
by
Emerging Contributor
Hi,
I'm new to python. I'd like to get a list of numbers (different flow accumulation thresholds for stream extraction) using arcpy.GetParameterAsText and then loop through the list, the relevant part of the code goes like this:

thresholdlist = arcpy.GetParameterAsText(2)
for threshold in thresholdlist:
    ...
    out_shp = out_prefix + threshold
    if not arcpy.Exists(out_shp):
        arcpy.gp.StreamToFeature_sa(strm, fdir, out_shp, "SIMPLIFY")


But when I enter
25, 35, 45
it seems it is taking one character at a time, because it create out2 and out5, then I got error
<class 'arcgisscriptin.ExecuteError'>: ERORR 000582: Error occurred during execution.

I am running ArcGIS 10 and I am running it as tool. Any suggestions/ideas? Many thanks!

Wei
Tags (2)
0 Kudos
4 Replies
DarrenWiens2
MVP Alum
I assume this is happening because your parameter is a string, and in python strings are lists themselves (lists of characters). A better way to attack this would be to use the split function.
thresholdlist = arcpy.GetParameterAsText(2)
splitthreshold = thresholdlist.split(",")
for threshold in splitthreshold:
...
0 Kudos
BruceNielsen
Frequent Contributor
If you need to use those values as integers and not strings, you can use this:
thresholdlist = arcpy.GetParameterAsText(2)
splitthreshold = [int(x) for x in thresholdlist.split(',')]
for threshold in splitthreshold:
...

See 'List Comprehensions' in the Python documentation for an explanation
0 Kudos
WeiLuo
by
Emerging Contributor
If you need to use those values as integers and not strings, you can use this:
thresholdlist = arcpy.GetParameterAsText(2)
splitthreshold = [int(x) for x in thresholdlist.split(',')]
for threshold in splitthreshold:
...

See 'List Comprehensions' in the Python documentation for an explanation


Great! Thanks! It worked!
0 Kudos
curtvprice
MVP Alum
I am running ArcGIS 10 and I am running it as tool. Any suggestions/ideas?


If you are running is a tool, you may want to make the input a multi-value integer. Then as the user enters them one a time in a dialog, they are all checked to be integers (within a domain that you can specify, for example, the tool won't accept negative numbers). This is really nice because you can do error checking before your tool even sees it, saving you script coding!

If you do it this way, the list comes across as a ';'-delimited string, so you need to split it this way:

strList = gp.GetParameterAsText(2)
lstNums = strList.split(';')


If the parameter is strings that may have quotes or ';' within the data values, you may need to get a little fancier with the "list comprehensions" mentioned in this thread.
0 Kudos