1

I am currently trying to get my update cursor to populate every row in each attribute table for 175 different feature class fields. I have written the script and it is populating the first row but all the next rows are coming out "NULL" (blank).

This is my script

import arcpy, csv

geodatabase = "C:\Users\kd16342\TSB_Values_Project\Tool\RasterTool.gdb"
csvfile = "C:\Users\kd16342\TSB_Values_Project\Documents\Scripts\ServicesforMosaic.csv" #change "Ecosystem Services" to "Ecosystem"

fields = ["Ecosystem","Cur_Con","Scale_Efft","Eff_Con","Recover","Typ_of_ser","Services","Eff_on_ser","Dur_s_aff","Reason","Eff_Durat","Unique_ID","Serv_ad_af","Serv_pe_af"] 

arcpy.TableToTable_conversion(csvfile, geodatabase, "ServicesforMosaic")

for x in range (9,176):
    with arcpy.da.SearchCursor(geodatabase+"//ServicesforMosaic",fields) as scursor:
        for rw in scursor:
            if rw[11] == str(x):
                with arcpy.da.UpdateCursor("C:\Users\kd16342\TSB_Values_Project\Tool\RasterTool.gdb\Unique_ID_"+x,fields) as ucursor:
                    for row in ucursor:
                        row[0] = rw[0]
                        row[1] = rw[1]
                        row[2] = rw[2]
                        row[3] = rw[3]
                        row[4] = rw[4]
                        row[5] = rw[5]
                        row[6] = rw[6]
                        row[7] = rw[7]
                        row[8] = rw[8]
                        row[9] = rw[9]
                        row[10] = rw[10]
                        row[11] = rw[11]
                        row[12] = rw[12]
                        row[13] = rw[13]
                        ucursor.updateRow(row)
                    del cursor, row

All helped welcomed!!!

10
  • Welcome to GIS SE. "...but all the next rows are coming out" Are they coming out blank? Mar 30, 2015 at 17:22
  • Ah sorry, yes first row will populate but all the other rows come out blank NULL
    – Kirsty Dee
    Mar 30, 2015 at 17:25
  • It's tough to judge what might be the problem the way you have posted your code. Could you clean it up with the Code Sample button? It would also be nice to get a screen shot of your tables. Mar 30, 2015 at 17:30
  • Are you running this in the Python window in ArcMap? You don't perhaps have one row selected in the feature class you are trying to update? Mar 30, 2015 at 17:30
  • @Cindy selection wouldn't be the cause of this since mapping layers are not being referenced in the script. Mar 30, 2015 at 17:36

1 Answer 1

0

You might try settings a WHERE clause on your Search Cursor instead. So, something like:

for x in range(9, 176):
    query = '"Unique_ID" = {0}'.format(x)
    with arcpy.da.SearchCursor(geodatabase + "/ServicesforMosaic", fields, query) as scursor:
        for rw in scursor:
            with arcpy.da.UpdateCursor("C:/Users/kd16342/TSB_Values_Project/Tool/RasterTool.gdb/Unique_ID_" + str(x), fields) as ucursor:
                for row in ucursor:
                    row[0] = rw[0]
                    row[1] = rw[1]
                    row[2] = rw[2]
                    row[3] = rw[3]
                    row[4] = rw[4]
                    row[5] = rw[5]
                    row[6] = rw[6]
                    row[7] = rw[7]
                    row[8] = rw[8]
                    row[9] = rw[9]
                    row[10] = rw[10]
                    row[11] = rw[11]
                    row[12] = rw[12]
                    row[13] = rw[13]

                    ucursor.updateRow(row)

Also, when using the with statement you don't need to del the row or cursor. So, you can remove del cursor, row from your code.

2
  • Hi there, I tried this but get the following error Traceback (most recent call last): File "C:/Users/kd16342/TSB_Values_Project/Documents/Scripts/OnlineHELPCursor.py", line 2, in <module> query = '"Unique_ID" = {1}'.format(x) IndexError: tuple index out of range
    – Kirsty Dee
    Mar 30, 2015 at 19:02
  • it should be '"Unique_ID" = {0}'.format(x) Mar 30, 2015 at 19:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.