AnsweredAssumed Answered

Help with Python Code

Question asked by Mick_D@ll@s on Jul 31, 2017
Latest reply on Jul 31, 2017 by Mick_D@ll@s

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

#======================================================================

Outcomes