How to convert arcpy.getparameterastext(0) as integer

2290
2
07-30-2011 10:51 AM
SreenivasaRaoPigili
Occasional Contributor
Hi All,
   I am getting the user input using the below statment.
Pop_option = arcpy.GetParameterAsText(0)
  I am trying to utilise this input value for my business logic.
  For this I have treat it as integer. So I have tried to convert this value into integer using below statement.
Pop_option = int(Pop_option)   and Pop_option = Pop_option.split(";")

But i am not getting desired datatype.
Can anyone help me on this??

Thanks in advance.
Tags (2)
0 Kudos
2 Replies
StacyRendall1
Occasional Contributor III
What exaclty is the user entering? Why do you split the string input? What do you mean by "I am not getting the desired datatype" - are you writing the value to a field and that is failing?

If they are just entering a number, i.e.
4568


all you have to do is convert from a string to an integer:
Pop_option_str = arcpy.GetParameterAsText(0)
Pop_option = int(Pop_option_str)


or to be on the safe side:
Pop_option_str = arcpy.GetParameterAsText(0)
try: Pop_option = int(Pop_option_str) # try conversion
except ValueError: arcpy.AddError('Input was not integer...') # input was invalid...


If they are entering something more complex, you will need to provide more information to us before we can help you...
0 Kudos
Luke_Pinner
MVP Regular Contributor
It sounds like you are expecting a multi-value parameter (e.g. '123;234;345').

In this case you can use list comprehensions, the map() function or a loop:
#List comprehension:
Pop_options = [int(p) for p in Pop_option.split(";")]

#Map function
Pop_options = map(int, Pop_option.split(';'))

#Loop
Pop_options=[]
for p in Pop_option.split(';'):
    Pop_options.append(int(p))
0 Kudos