Appending iterated rows to a dataset

I’ve got a script that works kinda/sorta like it should:

for row in eventDict:
fworkID = str(row[‘fwork-id’])
fworkCode = str(row[‘fwork-code’])
fworkDscr = str(row[‘fwork-dscr’])
fworkstart = str(row[‘start’])
fworkend = str(row[‘end’])
d1 = row[‘start’]
d2 = row[‘end’]
FMT = ‘%Y-%m-%d %H:%M:%S’
tdelta = str(datetime.strptime(d2, FMT) - datetime.strptime(d1, FMT))
fworkstatus = str(row[‘status’])

headers = [‘OP ID’, ‘Step ID’, ‘Description’, ‘Start’, ‘End’, ‘Elapsed’, ‘Status’]
rows = [
[ fworkCode, fworkID, fworkDscr, fworkstart, fworkend, tdelta, fworkstatus ] ,
]
dataset = system.dataset.toDataSet(headers, rows)
event.source.parent.getComponent(‘Test_Table’).data = dataset

It iterates through eventDict, and puts the last row into the dataset. Problem is, all the other rows aren’t in the dataset. I’m sure the issue is it isn’t “appending” the rows. I have no idea how to get there from here. I’m sure (hopeful?) the issue is simple, I just don’t know enough about python scripting. Any ideas? Thanks in advance!

You’re only creating one actual row list in the rows 2d array/list of lists. Try something like this:

# create holder list
rows = []

for row in eventDict:
  # for each row in the source, create a new empty row
  newrow = []
  # append each item, in order
  newrow.append(row['fwork-code'])
  newrow.append(row['fwork-id'])
  newrow.append((row['fwork-dscr'])
  newrow.append((row['start'])
  newrow.append((row['end'])
  d1 = row['start']
  d2 = row['end']
  FMT = '%Y-%m-%d %H:%M:%S'
  newrow.append((datetime.strptime(d2, FMT) - datetime.strptime(d1, FMT))
  newrow.append((row['status'])

  # add our new row (casting each element to a string) to the overall rows list
  rows.append([str(r) for r in newrow])

headers = ['OP ID', 'Step ID', 'Description', 'Start', 'End', 'Elapsed', 'Status']
dataset = system.dataset.toDataSet(headers, rows)
event.source.parent.getComponent('Test_Table').data = dataset

Ok cleaned it up and here’s the script:

# create holder list
rows = []

for row in eventDict:
# for each row in the source, create a new empty row
	newrow = []
# append each item, in order
newrow.append(row['fwork-code'])
newrow.append(row['fwork-id'])
newrow.append(row['fwork-dscr'])
newrow.append(row['start'])
newrow.append(row['end'])
d1 = row['start']
d2 = row['end']
FMT = '%Y-%m-%d %H:%M:%S'
newrow.append(datetime.strptime(d2, FMT) - datetime.strptime(d1, FMT))
newrow.append(row['status'])

# add our new row (casting each element to a string) to the overall rows list
rows.append([str(r) for r in newrow])

headers = ['OP ID', 'Step ID', 'Description', 'Start', 'End', 'Elapsed', 'Status']
dataset = system.dataset.toDataSet(headers, rows)
event.source.parent.getComponent('Test_Table').data = dataset

It still only displays the last row in the table…

The section under “# append each item, in order” needs to be indented to make it part of “for row in eventDict:”. As shown above, the script sets newrow = [] for each item in eventDict, and then goes onto the next section after row is set to the last row, which is why you only get the last row.

1 Like

Bingo! I now have multiple rows. Outstanding and thanks for the help!