18

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?

45

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))

3

GetCount returns a Result object and not an integer or a string.

To get a string you use the getOuput method of the result object and pull its first part. To see any other parts try switching the 0 for 1, 2, etc.

If you need to turn that string into an integer then use an int() function.

To learn more about the Result object and its getOutput method the Online Help should be consulted.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.