Convert CSV to Dataset

This code will convert a CSV file to a Ignition Dataset.

def readCsvAsDataSet(path):
	from java.io import BufferedReader,FileReader,File
	from system.dataset import toDataSet
	br = BufferedReader(FileReader(path))

	line = br.readLine()

	headers = line.split(',')

	line = br.readLine()

	data = []
	while line != None:
		data.append(line.split(','))
		line = br.readLine()

	return toDataSet(headers,data)

To use it, you would do something like

path = system.file.saveFile(".csv")
dataset = app.file.readCsvAsDataSet(path)
event.source.getComponent("Table").data = dataset

Thanks for posting this. Readers should also be aware that as of 7.2 there are these two functions available:

[tt]system.dataset.toCSV(Dataset)[/tt]
and
[tt]system.dataset.fromCSV(String)[/tt]

Hi

Is it possible to use UTF8 encoding with this function?
Now all special characters goes wrong.

BR
Tommi

I’m assuming you’re talking about Kyle’s script. You’d use UTF-8 like so:

 br = BufferedReader(InputStreamReader(FileInputStream(path),"UTF-8"))