20

I am using ArcMap 10.1.

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?

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
Erica
  • 8,974
  • 4
  • 34
  • 79

3 Answers3

51

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

ccn
  • 2,919
  • 20
  • 19
  • Esri implements the Result object such that you can use the Python indexing operator to get outputs: int(arcpy.GetCount_management(Path_pts)[0]) – bixb0012 Feb 16 '22 at 23:48
3

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

To get a string you use the getOutput 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.

PolyGeo
  • 65,136
  • 29
  • 109
  • 338
-2

Just use str(TotalPoints) in your msg. Incase, if the count needs to be added with other integer value in future then just use int(str(TotalPoints))

Kadir Şahbaz
  • 76,800
  • 56
  • 247
  • 389
Zamil
  • 1
  • A Result object cannot be cast as a string (I've learned quite a bit about it in the last 7 years). The other answers help explain why. Thank you though! – Erica Apr 15 '20 at 23:27