Dataset not populating table

Hello, I used code which works for 4 other files/tables. I created a table. And i created the dataset. But for some odd reason my table remains empty.

import csv

#Monday Import
file = open("\\location\company Data\Monday_Data.csv")
csvData = csv.reader(file)

header = csvData.next()
pyData = []
for row in csvData:
pyData.append(row)
finalData = system.dataset.toDataSet(header,pyData)
event.source.parent.getComponent(‘Monday Table’).data = finalData

Two things, you need to put your code in a preformatted text section you enclose your code in 3 `'s before and after the code so it will maintain formatting. There is also a button that looks like </> that would create the space. The other thing, if it isn’t working, is it giving any error in your console? If so showing that error will help point to the issue.

I get a blank error i cant even open but see down at the bottom of the screen.

That code worked for another file. So i just replicated it. But someone else did originally do it, so I’m tryin to clean it up.

this code is in the script editor for a button.

i don’t know what </> button you are talking about.

Are you testing it in the designer or in the project? If you test it in the designer you can open the console and errors will post to it.

And for that symbol I’m talking about in the reply window at the top of it:

also when i put ``` it will not let me close the script editor

I’m testing in designer
i get this error

ERROR [ActionAdapter-AWT-EventQueue-2] <HTML>Error executing script for event:&nbsp;<code><b>actionPerformed</b></code><BR>on component:&nbsp;<code><b>Button</b></code>.
Traceback (most recent call last):
  File "<event:actionPerformed>", line 59, in <module>
IOError: (21, 'Is a directory', '')

In your button’s action performed script, what do you have on line 59 in the script editor?

this is on that line

file = open("\\location\company Data\Monday_Data.csv")

i made an adjustment for indexing and got this error

ERROR [ActionAdapter-AWT-EventQueue-2] <HTML>Error executing script for event:&nbsp;<code><b>actionPerformed</b></code><BR>on component:&nbsp;<code><b>Button</b></code>.
Traceback (most recent call last):
  File "<event:actionPerformed>", line 66, in <module>
IndexError: Row 0 doesn't have the same number of columns as header list.


I has extended to first line to have as many items as the most filled row and am using header = csvData.next() which should take care of that. but something is funky

It doesn’t like the path your using, try adding an extra \ with each.

file = open("\\location\\company Data\\Monday_Data.csv")

To explain it, the \ is an escape character for python. By doubling it up it will treat it as a backslash which is what your looking for. A forward slash may also work as a fix.

The weird part is I use that exact same code for 3 other files. And it places all of them in the table and lets me pull from them. But for some weird reason with this file is not working. i tried a combo of slashes but no luck.
this is the last error i got

ERROR [ActionAdapter-AWT-EventQueue-2] <HTML>Error executing script for event:&nbsp;<code><b>actionPerformed</b></code><BR>on component:&nbsp;<code><b>Button</b></code>.
Traceback (most recent call last):
  File "<event:actionPerformed>", line 66, in <module>
IndexError: Row 0 doesn't have the same number of columns as header list.


Line 66 is a different error than the original one. I’m assuming it lines up with your system.dataset.toDataSet(). If your headers and your rows under it don’t match it will do that. What I would do is add a print for your header and a print for row in your loop and look at the data returned to make sure it is processing it how you think.

import csv

#Monday Import
file = open("\\location\\company Data\\Monday_Data.csv")
csvData = csv.reader(file)

header = csvData.next()
print header
pyData = []
for row in csvData:
     pyData.append(row)
     print row
finalData = system.dataset.toDataSet(header,pyData)
event.source.parent.getComponent(‘Monday Table’).data = finalData

Also where it lists row 0, I’m wondering do you have an empty row between your header and your data in your CSV. I’d look specifically at that row to start but it has to line up all the way through or it will throw that error. You could also potentially have an empty column that has a header but doesn’t have any data in it at for the last column.

The console made it through row 0 and 1 printing then threw the error. But I did notice know the header did not cover all the columns. so I extended it . but not every row has data in it for that column. Do I need data in every row and column? Could I program a fail safe. if row col null insert NA or something?

I know the other file has blanks in rows for cols, so not sure why this one is being so nit picky

Each row of data must have the same number of elements as the list of headers (column names). You can supply a python None for null, but you may have flaky behavior if any nulls are in the first row. The toDataSet() function determines column data types from the first row, and will use String by default. If you cannot guarantee a non-null first row, you should use the underlying platform DatasetBuilder tool, so you can explicitly set the column data types. There are numerous examples on this forum.

1 Like

I did find some extended rows further than what I realized. I am gonna to try and extend the header to see if that solves it

i still get

Traceback (most recent call last):
  File "<event:actionPerformed>", line 68, in <module>
IndexError: Row 0 doesn't have the same number of columns as header list.


code is

file = open("\\\\location\\datasource$\\location\\Item_Master.csv")
csvData = csv.reader(file)

header = csvData.next()
print header
pyData = []
for row in csvData:
	pyData.append(row)
	print row
	finalData = system.dataset.toDataSet(header,pyData)
	event.source.parent.getComponent('Item Master').data = finalData

fixed by having IT clean the .csv file up. Finally got the data in. Still having some issues but I created a separate discussion for that.

thank you

link to new discussion with following issue. any help appreciated

http://forum.inductiveautomation.com/t/import-button-silently-running-vs-opening-window-not-working-anymore/43334/4