Newby Needs Help with Python code

Rami Chowdhury rami.chowdhury at merton.oxon.org
Wed Sep 1 13:28:37 CEST 2010


Hi Esther,

On Wed, Sep 1, 2010 at 13:29, Nally Kaunda-Bukenya <nkaunda at yahoo.com> wrote:
> #THE PROGRAM:
> import arcgisscripting
> gp=arcgisscripting.create()
> gp.Workspace = "C:\\NPDES\\NPDES_PYTHON.mdb"
> fc = "Outfalls_ND"
>
> try:
>     # Set the field to create a list of unique values
>         fieldname = "OUTFALL_ID"
>
>     # Open a Search Cursor to identify all unique values
>         cur = gp.UpdateCursor(fc)
>         row = cur.Next()
>
>         # Set a list variable to hold all unique values
>         L = []
>
>         # Using a while loop, cursor through all records and append unique
>         #values to the list variable
>         while row <> None:
>             value = row.GetValue(fieldname)
>             if value not in L:
>                 L.append(value)
>             row = cur.Next()
>             row.SetValue(Tot_Outf_Area, sum(row.AREA_ACRES)) #total area of
> each outfall=sum of all area 4 each unique outfallid
>             cur.UpdateRow(row) #to commit changes
>             row=cur.Next()
>             print row.Tot_Outf_Area
>         # Sort the list variable
>         L.sort()
>
>         # If a value in the list variable is blank, remove it from the list
> variable
>         #to filter out diffuse outfalls
>         if ' ' in L:
>             L.remove(' ')
>
> except:
>     # If an error occurred while running a tool, print the messages
>     print gp.GetMessages()


Have you tried running this code? I suspect it won't work at all --
and because you are catching all possible exceptions in your
try...except, you won't even know why. Here are the things that I'd
suggest, just from glancing over the code:
 - Remove the try...except for now. Getting an exception, and
understanding why it occurred and how best to deal with it, is IMHO
very helpful when prototyping and debugging.
 - Take another look at your while loop. I don't know ArcGIS, so I
don't know if the UpdateCursor object supports the iterator protocol,
but the normal Python way of looping through all rows would be a for
loop:

for row in cur:
    # code

For example, you are calling cur.Next() twice inside the loop -- is
that what you want?

Hope that helps,
Rami


>
>
>
> #Please Help!!!
>
> #Esther
>
>
>
> --
> http://mail.python.org/mailman/listinfo/python-list
>
>



-- 
Rami Chowdhury
"Never assume malice when stupidity will suffice." -- Hanlon's Razor
408-597-7068 (US) / 07875-841-046 (UK) / 0189-245544 (BD)



More information about the Python-list mailing list