Issue with script to read CSV into table

I'm trying this code to import data from csv into database and works fine.
I don't need to populate a table with the data, so I'm trying to avoid this step, but there is an error I can't solve. This is the code:

#1. Read data
csvData = csv.reader(open(system.file.openFile('csv')))
#2. Populate table with data
event.source.parent.getComponent('Table').data = system.dataset.toDataSet(csvData.next(), list(csvData))

# Create a dataset with the header and the rest of our CSV.
dataset1 = event.source.parent.getComponent('Table').data
dataset2 = system.dataset.toDataSet(csvData.next() ,list(csvData))

dataset1 and dataset2 should be the same, as I assign in point 2 the data from csv.
but creating dataset2 shows this error:
Caused by: org.python.core.PyException: StopIteration

Do you know why two datasets are not equal? I think I'm missing something.

thanks!

CSV reader objects are stateful. The first time, you call next() to get the header row, then you call list() which consumes the rest of the CSV document.

Then you try to do it a second time, but the reader has already given you all it can - that's why you're getting a StopIteration error.

1 Like