Newby Needs Help with Python code

Peter Otten __peter__ at
Wed Sep 1 18:52:09 CEST 2010

Nally Kaunda-Bukenya wrote:

> I hope someone can help me. I am new to Python and trying to achive the
> following:
> 1)      I would like to populate the Tot_Ouf_Area field with total area of
> each unique outfall_id (code attempted below,but Tot_Ouf_Area not
> populating) 
> 2)      I would also like to get the user input of Rv ( each
> landuse type will have a specific Rv value). For example the program
> should ask the user for Rv value of Low Density Residential (user enters
> 0.4 in example below and that value must be stored in the Rv field), and
> so on as shown in the 2nd table below…

I don't know arcgis, so the following is just guesswork.
I iterate over the Outfalls_ND table twice, the first time to calculate the 
sums per OUTFALL_ID and put them into a dict. With the second pass the 
Tot_Outf_Area column is updated

import arcgisscripting

def rows(cur):
    while True:
        row = cur.Next()
        if row is None:
        yield row

gp = arcgisscripting.create()
gp.Workspace = "C:\\NPDES\\NPDES_PYTHON.mdb"

TABLE = "Outfalls_ND"
TOTAL = "Tot_Outf_Area"

aggregate = {}
cur = gp.UpdateCursor(TABLE)
for row in rows(cur):
    group = row.GetValue(GROUP)
    amount = row.GetValue(SUM)
    aggregate[group] = aggregate.get(group, 0.0) + amount

cur = gp.UpdateCursor(TABLE)
for row in rows(cur):
    group = row.GetValue(GROUP)
    row.SetValue(TOTAL, aggregate[group])

As this is written into the blue it is unlikely that it runs successfully 
without changes. Just try and report back the results.


More information about the Python-list mailing list