Convert CSV to Dataset

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

def readCsvAsDataSet(path):
	from 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:
		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:



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


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

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