dbecker88

several search/update cursors together

Discussion created by dbecker88 on Mar 17, 2011
Hi everyone, need  a little python help here...

I have about 160 gdb's, all with almost identical schema.
I need to loop through all the feat. classes in the gdb's and update individual rows in the attribute tables.

The "Photos" field needs updating based on the contents of the "facil_id" field. So, we grab the "facil_id" value, then use that value to find a "facil_name" in a lookup table. Finally, we are able to modify the "Photos" column.

I have the script running, but if the 'old' contents of the "Photos" field is in mutiple records, it grabs the "facil_name" twice??

If someone could look over the .py I'd appreciate it!

thanks,

for path2wrksp in path2wrksps:
    #print "... " + path2wrksp
    gp.workspace = path2wrksp

    wrksps3 = gp.ListWorkspaces("GIS_20110315*","FileGDB")
    for wrksp3 in wrksps3:
        gp.workspace = wrksp3
        #print "...... " + gp.workspace
        datasets = gp.ListDatasets("*")        
        for dataset in datasets:
            gp.workspace = wrksp3 + "\\" + dataset
            print dataset
            FcList = gp.ListFeatureClasses("*")
            for fc in FcList:
                gp.workspace = dataset + "\\" + fc
                rows = gp.SearchCursor(fc)
                row = rows.next()
                for row in iter(rows.next, None):
                    #if Photos field is null, skip it    
                    if row.Photos == None:
                        print "null"
                    else:
                        fields = gp.ListFields(fc)
                        for field in fields:
                            if field.Name.lower() == "facil_id":

                                #print row.GetValue("facil_id")
                                gdb_facil_id = row.GetValue("facil_id")
                                #print gdb_facil_id
                                lookuptbl = r"P:\\USACE Mobile 0610-01 2010 63D RSC NR Contract\\03-Source Documents\\GIS\\SITE_ID_LOOKUP.mdb\\facils"
                                q = '[facil_id_in_lookup] = ' + "'" + gdb_facil_id + "'"
                                #print q
                                rows2 = gp.SearchCursor(lookuptbl, q)
                                for row2 in iter(rows2.next, None):
                                    #print gdb_facil_id, row2.GetValue("facil_name")
                                    facil_name = row2.GetValue("facil_name")
                                    #print facil_name
                            elif field.Name.lower() == "photos":
                                
                                calcfield = field.Name
                                old = row.GetValue("Photos")
                                oldvalue = '"' + calcfield + '" = ' + "'" + old + "'"
                                tmpvalue = str(old[3:])
                                newvalue = ".." + os.sep + facil_name + os.sep + tmpvalue
                                rows3 = gp.UpdateCursor(fc, oldvalue)
                                #row3 = rows3.Next()
                                for row3 in iter(rows3.next, None):
                                    row3.SetValue("Photos",newvalue)
                                    rows3.UpdateRow(row3)
                                    print gdb_facil_id + " " + fc + " photos updated"
                                    row3 = rows3.Next()
                            
                                del row3, rows3
                            
                    row3 = None
                    rows3 = None
                    row2 = None
                    rows2 = None 
                    row = None
                    rows = None

Outcomes