Igor
Igor

Reputation: 11

CSV data with 2 different delimiters for Python 2/Ignition

I am developing the code in Ignition software using Jython/Python 2 scripts. We need to read data from csv file that has two delimiters "," in header and "\t" in data. The code we use are:

file_path = r'T:\test1.csv'
csvData = csv.reader(open(file_path, 'r'))
header = csvData.next() # Skip the fist row
dataset = system.dataset.toDataSet(header,list(csvData))
calcwindow.rootContainer.getComponent('Power Table').data = dataset

After applying this code we get this: Power Table

Question are how can we separate the data so that all rows and columns match with csv.reader as ignition do not support panda or re :(

Update the code and now it separate data correctly:

csvData = csv.reader(open(file_path, 'r'),delimiter=',')
header = csvData.next()# Skip the fist row
for line in csvData:
    str1 = "".join(line) #removes commas
    #print str1
    parts = str1.split("\t")
    print parts
dataset = system.dataset.toDataSet(header,list(parts))
calcwindow.rootContainer.getComponent('Power Table').data = dataset

, but the error code came up:

Row 0 doesn't have the same number of columns as header list.

Any suggestions??

Thanks Igor

Upvotes: 0

Views: 479

Answers (1)

Igor
Igor

Reputation: 11

I figure it out myself.

Here is the code:

        file_path = r'T:\test1.csv'
    try:
        file = open(file_path)
        csvData = csv.reader(file,delimiter=',') # open the file with comma delimiter
        header = csvData.next()# Skip the fist row
        csvData1 = list(csvData) # create list from data
        lstLine = csvData1[-1] # selects last line added
        str1 = "".join(lstLine) #removes commas and create string
        parts = str1.split("\t") #split string back into list
        dataset = system.dataset.toDataSet(header,[parts])
        calcwindow.rootContainer.getComponent('Power Table').data = dataset
        file.close()
    except:
        print "CSV busy exporting from TIA software"

Hope it will help anyone.

Upvotes: 0

Related Questions