It is currently Mon Sep 01, 2014 5:44 am




Post new topic Reply to topic  [ 4 posts ] 
 Convert CSV to Dataset 
Author Message
General
General

Joined: Sun Feb 25, 2007 2:54 pm
Posts: 567
Post Convert CSV to Dataset
This code will convert a CSV file to a Ignition Dataset.
Code:
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
Code:
path = system.file.saveFile(".csv")
dataset = app.file.readCsvAsDataSet(path)
event.source.getComponent("Table").data = dataset


Wed Jan 12, 2011 8:40 pm
Profile WWW
Moderator
Moderator

Joined: Sun Apr 02, 2006 2:46 pm
Posts: 4053
Location: Sacramento, CA
Post Re: Convert CSV to Dataset
Thanks for posting this. Readers should also be aware that as of 7.2 there are these two functions available:

system.dataset.toCSV(Dataset)
and
system.dataset.fromCSV(String)

_________________
Carl Gould
Co-Director, Software Engineering
Inductive Automation


Sat Jan 22, 2011 3:20 pm
Profile WWW
Captain
Captain

Joined: Thu Apr 15, 2010 8:22 am
Posts: 99
Location: Tampere, Finland
Post Re: Convert CSV to Dataset
Hi

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

BR
Tommi


Wed Jan 04, 2012 9:48 am
Profile WWW
Moderator
Moderator

Joined: Sun Apr 02, 2006 2:46 pm
Posts: 4053
Location: Sacramento, CA
Post Re: Convert CSV to Dataset
I'm assuming you're talking about Kyle's script. You'd use UTF-8 like so:

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

_________________
Carl Gould
Co-Director, Software Engineering
Inductive Automation


Fri Feb 10, 2012 3:24 pm
Profile WWW
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 


Who is online

Users browsing this forum: No registered users and 0 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to: