Using and Search and Update Data Access Cursors to Populate a Field in a New Table

3393
16
Jump to solution
01-29-2018 02:52 PM
TessOldemeyer
Occasional Contributor

I'm assuming this is pretty straightforward, and I am missing something fairly simple. 

I am attempting to bring in a column of data values, 'FieldA', from a table, 'Table_1', to populate an empty field, 'FieldA', in a new, empty table, 'Table_2'. These 'FieldA' values will be used as a key in 'Table 2' for subsequent field calculations. Is there a direct way to do this?

I have been attempting to use a search cursor and update cursor combination to write the field values to the new table, though I am not sure how to successfully employ the cursors to update the single field without an existing key between the two tables (where I would use a dictionary). 

Here is my unsuccessful, botched code:

search_feats = arcpy.da.SearchCursor(Table_1,["FieldA"])


i = 0
with arcpy.da.UpdateCursor(Table_2,["FieldA"]) as upd_cur:
     for upd_row in upd_cur:
         
         i=i+1
         upd_row[0] = search_feats (i)
         upd_cur.updateRow(upd_row)  
         
del upd_cur
‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍‍

What may be a better way to approach this?

0 Kudos
16 Replies
RandyBurton
MVP Alum

I believe that with an insert cursor, the field list used at line 7 should include all the field names.  Assuming your table only has one field (FieldA), the list can be only 1 item.

In line 10, the "None" was the value to assign to the "MyCalc" field.  Since it is not part of the field list in line 7, it should be dropped.  Again, if only one field, something like :

inCursor.insertRow((row)) # field values to insert

Where the values in tbList what you were expecting?

0 Kudos
TessOldemeyer
Occasional Contributor

Gotcha. Yes, Table_2 currently has 40 empty fields. I would like to only populate FieldA. Is there a way to assign 'None' to all subsequent fields without having a long list of 'Nones'? It looks like I might be able to use (*) to read in all fields from Table_1 if this is needed for line 7 (vs. having to list out all fields).

Also, the tbList values are what I was expecting. I just wanted to make sure the list format wasn't part of the 'size of row error'.

Thanks for all of your help!

0 Kudos
RandyBurton
MVP Alum

Since you only have one field value to insert, try writing the insert cursor part like this:

rows = arcpy.InsertCursor("Table_2")
for item in tbList:
    row = rows.newRow()
    row.setValue("FieldA", item) # may need to format "item"
    rows.insertRow(row)‍‍‍‍‍‍‍‍‍‍
0 Kudos
RandyBurton
MVP Alum

In my previous response, I used the old InsertCursor.  Try the da.InsertCursor, and since it is only one field, not a tuple, just one set of () around row.

cursor = arcpy.da.InsertCursor("Table_2",['FieldA'])
for row in tbList:
    cursor.insertRow([row]) # as Bixby suggested below, using square brackets
del cursor‍‍‍‍‍‍‍‍
0 Kudos
TessOldemeyer
Occasional Contributor

This is great, and I like the da.InsertCursor route. I am getting a "argument must be a sequence of values" error on line 3 (line 9 in the code below). 

Here is the code I have:

tbList = [] # empty list

for row in arcpy.da.SearchCursor("Table_1", ["FIELD_A"]):
    if row[0] not in tbList:
        tbList.append(row[0]) # copy distinct rows to list

cursor = arcpy.da.InsertCursor("Table_2",['FIELD_A'])
for row in tbList:
    cursor.insertRow(row)
del cursor

0 Kudos
JoshuaBixby
MVP Esteemed Contributor

Try:

cursor.insertRow([row])

TessOldemeyer
Occasional Contributor

That did the trick. Thank you so much!

0 Kudos