Need help with a calculate Python Print Statement script for ArcMap

668
1
Jump to solution
11-05-2016 08:38 AM
Noah_McLaughlin
New Contributor II

To finish off my script tool i want the script to give me an add message (sum) that calculates total hectares for only 2 certain attributes (soil classes 1 and 2) in my Hectares field. I have a hectares field and a field that give soil classes 1 to 5. in the python language how would i write the expression?

The shapefile i want to perform this on is called: [clip_output]  

The field thats lists soil type is called: [CLASS]    

The hectares field that I want the script to give me the sum from is called [Hectares]  

This is what I have so far but it is not working (not sure if i need to list variables):

with arcpy.da.SearchCursor(clip_output, ['Hectares'], 'CLASS in (1, 2)') as cur:
   sum = 0
   for row in cur:
      sum += row[0]

arcpy.AddMessage(sum)

error im getting is: 

line 106, in <module> for row in cur: RuntimeError: An invalid SQL statement was used

0 Kudos
1 Solution

Accepted Solutions
JoshuaBixby
MVP Esteemed Contributor

Is CLASS a string or number field?  If the former, it will generate the same error you are seeing when you try to treat it like a number, which you are doing.  If the field is a string, try the following:

with arcpy.da.SearchCursor(clip_output, ['Hectares'], "CLASS in ('1', '2')") as cur:

View solution in original post

1 Reply
JoshuaBixby
MVP Esteemed Contributor

Is CLASS a string or number field?  If the former, it will generate the same error you are seeing when you try to treat it like a number, which you are doing.  If the field is a string, try the following:

with arcpy.da.SearchCursor(clip_output, ['Hectares'], "CLASS in ('1', '2')") as cur: