Help with Python Code

738
6
07-31-2017 09:19 AM
MickSurber
New Contributor II

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

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

0 Kudos
6 Replies
RebeccaStrauch__GISP
MVP Emeritus

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 

0 Kudos
MickSurber
New Contributor II

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!

0 Kudos
DanPatterson_Retired
MVP Emeritus

before you start coding, make sure Summary Statistics ... doesn't do what you want

MickSurber
New Contributor II

Thanks very much for your input Dan.  I review the content of your link.

0 Kudos
MitchHolley1
MVP Regular Contributor

Don't forget to add an 'r' in front of your path strings. 

fc = r"C:\Users\surbemi\Desktop\New folder\Export_Output.shp"

MickSurber
New Contributor II

Thanks for the reminder Mitch.  I do often forget that !

0 Kudos