I am trying to use an InsertCursor/SeachCursor to transfer data from one dataset to another wile incorporating a repeating tuple. The first dataset (in the below code is "Forest_Surface_Fuels") contains all the data and has attribute fields for each record that contains data for 1hr, 10hr, and 100hr fuels. What I am trying to do as I transfer this data to a empty dataset ("1_10_100") is to not only take the values in those fields, but create a master list for all records in a project and in a new field state whether the data is a 1hr, 10hr, or 100hr.
Here is the code that I have:
# Fields that data will be inserted into in the final excel table
fields = ['Plot_', 'SurveyType', 'Transect', 'OneHour', 'TenHr', 'HunHr']
#The data to be inserted (this is the tuple that will be repeated)
fuelsTuple = [
('Macroplot', "Pre", "1", 'onehr_pre_1', 'tenhr_pre_1', 'hundhr_pre_1'),
('Macroplot', "Pre", "2", 'onehr_pre_2', 'tenhr_pre_2', 'hundhr_pre_2'),
('Macroplot', "Post", "1", 'onehr_post_1', 'tenhr_post_1', 'hundhr_post_1'),
('Macroplot', "Post", "2", 'onehr_post_2', 'tenhr_post_2', 'hundhr_post_2')
]
#make count of times to repeat the tuple
repeats = arcpy.management.GetCount("Forest_Surface_Fuels")
count = int(repeats[0])
#Insert Cursor to update fields
plotCursor = arcpy.da.InsertCursor("Forest_Surface_Fuels", fuelsTuple)
with arcpy.da.SearchCursor("1_10_100", fields) as cursor:
for i in range(count):
for row in cursor:
plotCursor.insertRow(row)
del plotCursor
del cursor
When I run this, I get this error:
Thanks!
You’re very close, but the cursors are flipped and you’re passing the tuple list as “fields” (that’s why it blows up).
SearchCursor should read from Forest_Surface_Fuels (source)
InsertCursor should write to 1_10_100 (target)
InsertCursor("Forest_Surface_Fuels", fuelsTuple) is wrong because the 2nd argument must be a list of field names, not row data.
If your goal is: for each input feature, create 4 rows (Macroplot Pre/Post x Transect 1/2) and pull values from those named fields, do this pattern:
out_fields = ['Plot_', 'SurveyType', 'Transect', 'OneHour', 'TenHr', 'HunHr']
templates = [
("Pre","1", "onehr_pre_1","tenhr_pre_1","hundhr_pre_1"),
("Pre","2", "onehr_pre_2","tenhr_pre_2","hundhr_pre_2"),
("Post","1","onehr_post_1","tenhr_post_1","hundhr_post_1"),
("Post","2","onehr_post_2","tenhr_post_2","hundhr_post_2"),
]
# read Plot_ plus all fuel fields you reference
src_fields = ['Plot_'] + sorted({f for t in templates for f in t[2:]})
with arcpy.da.SearchCursor("Forest_Surface_Fuels", src_fields) as sc, \
arcpy.da.InsertCursor("1_10_100", out_fields) as ic:
idx = {name:i for i,name in enumerate(src_fields)}
for r in sc:
plot_id = r[idx['Plot_']]
for survey, tran, f1, f10, f100 in templates:
ic.insertRow([plot_id, survey, tran, r[idx[f1]], r[idx[f10]], r[idx[f100]]])
That gives you the “master table” (one row per plot per survey/transect) with 1/10/100 hr values populated correctly.