Friday 25 May 2018

arcgis 10.1 - Casting ArcPy result object from arcpy.GetCount_management() as integer instead?


I am trying to get a number by counting how many points are in a shapefile. And this works, except I then am running into trouble using that number somewhere else. Eventually, I'll be using that count in some math (field calculator), but while debugging I'm running into an error that will end up causing me trouble later on.


This code:



TotalPoints = arcpy.GetCount_management(Path_Pts)
arcpy.AddMessage(">>>> PROCESS: COUNT PATH POINTS {" + TotalPoints + "}")

gives this error:


TypeError: cannot concatenate 'str' and 'Result' objects

I tried casting it as a int, which it ALSO doesn't like:


TypeError: int() argument must be a string or a number, not 'Result'

So I've got a 'Result' object and need to turn it into a number.



How can I do that -- or is using the ArcPy function unnecessary or overly complicated here?



Answer



Use the following method on the Result object and you'll be able to cast as int:


.getOutput(0) will return the value at the first index position of a tool.


int(arcpy.GetCount_management(Path_Pts).getOutput(0))


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...