I'm trying to create a script that will traverse through a giant attribute table (comprised of the records of 68 polygons), and total up the values in a particular field for features within each polygon. I want the script to go through the table, look for polygon 1 records, add all values in the "Grand_Tota" field, the move on to looking for all records that comprise polygon 2, and so on.
I haven't used Lists too often so perhaps my problem lies there.
When I run the code now, it just spits keeps spitting out the first total (5).
Each polygon feature is weirdly named "Location X : 0 - 5" where X is the loc number.
#======================================================================
import arcpy
fc = "C:\Users\surbemi\Desktop\New folder\Export_Output.shp"
field = "Grand_Tota"
sCursor = arcpy.SearchCursor(fc)
loc = 1
polyName = "Name" # Names are formatted ("Location X : 0 - 5") where X = loc
sum = 0
total = []
while loc < 21:
for row in sCursor:
if row.getValue(polyName) == "Location " + str(loc) + " : 0 - 5":
i = row.getValue(field)
sum = sum + i
total.append(sum)
print "Location " + str(loc) + " Total: " + str(int(total[loc-1]))
sum = 0
loc = loc + 1
#======================================================================
tagging Python for more exposure
Is this a script you need to do for a class? If not, there may be easier ways to do this that do not require a cursor or even a python script. But if you do need a cursor/script, you may want to look at the .da module for your cursors What is the data access module?—Help | ArcGIS Desktop
Thanks for the speedy reply Rebecca ! It's for a work problem and using a cursor to check each row of an attribute table is the only way I know how .... I will review the link you provided. Thanks very much!
before you start coding, make sure Summary Statistics ... doesn't do what you want
Thanks very much for your input Dan. I review the content of your link.
Don't forget to add an 'r' in front of your path strings.
fc = r"C:\Users\surbemi\Desktop\New folder\Export_Output.shp"
Thanks for the reminder Mitch. I do often forget that !