Generate dynamic CSV using script

Hi All,
Ignition v7.9

I’m using an event script to save a CSV file to disk. The CSV has a header which is built up from several different tags, depending on the configuration of the machine in question.

I have created a data set with 5 columns (the maximum width of my CSV) and I am appending data to this. All the data in this header is acquired from OPC tags. At the end of this process I convert the data set and save to a CSV.

The last line of the CSV has the headers for the section that will contain the process data. This data is currently stored in an SQL server. The next part of the project will be to fetch the SQL data and write it to the bottom of the file.

Questions:

  1. Is there a more elegant way to achieve the same result?
  2. Is there a good way to append SQL data to the end of an existing CSV? I have previously used “dataset to CSV” functions previously to create a new CSV.
  3. When I store a value of 1.44 in the data set, it is saved as 1.44000005722. How do I fix this?

Thanks!

John

Declare the dataset.

Headers = [1,2,3,4,5]
Data = []

Data.append([“File Name”,FileName,"","",""])
Data.append([“Date”,Date,"","",""])
Data.append([“Time”,Time,"","",""])

Pasted grid

if GridType == 1:
Data.append([“N Number”,str(system.tag.read(“ETHICK/Command/N_Nbr”).value),"","",""])
Data.append([“Grid/elec weight”,str(system.tag.read(“ETHICK/Command/Weight_grid”).value),"","",""])

Unpasted grid

elif GridType ==2:
Data.append([“N Number”,str(system.tag.read(“ETHICK/Command/N_Nbr”).value),"","",""])
Data.append([“Grid/elec weight”,str(system.tag.read(“ETHICK/Command/Weight_grid”).value),"","",""])

Fabric characterisation

elif GridType ==3:
Data.append([“CF Number”,str(system.tag.read(“ETHICK/Command/CF_Nbr”).value),"","",""])
Data.append([“Felt weight”,str(system.tag.read(“ETHICK/Command/Weight_Felt”).value),"","",""])

Calibration

elif GridType ==4:
Data.append(["","","","",""])
Data.append(["","","","",""])

Data.append([“Grid size”,str(system.tag.read(“ETHICK/Recipe/GridSize”).value),"","",""])
Data.append([“Tab orientation”,str(system.tag.read(“ETHICK/Recipe/TabOrientation”).value),"","",""])
Data.append([“Scan mode”,“Continuous”,"","",""])
Data.append([“Target thickness”,str(system.tag.read(“ETHICK/Command/Thickness_Target”).value),"","",""])
Data.append([“Grid type”,GridDesc,"","",""])
Data.append([“Recipe”,str(system.tag.read(“ETHICK/Recipe/RecipeName”).value),"","",""])
Data.append([“Y points”,str(system.tag.read(“ETHICK/Recipe/PointsY”).value),"","",""])
Data.append([“Move speed”,str(system.tag.read(“ETHICK/Recipe/SpeedMove”).value),"","",""])
Data.append([“X point origin”,str(system.tag.read(“ETHICK/Recipe/PosXStart”).value),"","",""])
Data.append([“X point end”,str(system.tag.read(“ETHICK/Recipe/PosXStop”).value),"","",""])
Data.append([“Y point origin”,str(system.tag.read(“ETHICK/Recipe/PosYStart”).value),"","",""])
Data.append([“Y point pitch”,str(system.tag.read(“ETHICK/Recipe/PosXStop”).value),"","",""])
Data.append(["","","","",""])
Data.append([“Y point #”,“Y position (mm)”,“X position (mm)”,“Upper sensor(mm)”,“Lower sensor (mm)”,“Thickness (mm)”])

Dataset = system.dataset.toDataSet(Headers, Data)

Dataset2 = system.dataset.toCSV(dataset = Dataset, showHeaders = 0, forExport = 0)

system.file.writeFile(FilePath, Dataset2)

It looks to me like that dataset is unnecessary…i would use the python csv library…

import csv

ofile = open(FilePath, 'a')  # open the file in append mode
writer = csv.writer(ofile, delimiter=',', lineterminator='\n')
writer.writerows(Data)  # write the list of lists all at once
# writer.writerow(olist) to append just one row to the csv file
ofile.close()  # make sure to close your file!

also, rather than appending over and over again, I would do something like this in each block of code:

Data = []

Data += [
    [“File Name”,FileName,"","",""]
    ,[“Date”,Date,"","",""]
    ,[“Time”,Time,"","",""]
]

not super sure what that 1.440000… thing is, but you might try using python’s

round(1.44000005722, 3)
2 Likes

Awesome!

Thanks for the quick reply. This has cleaned up my code a lot and works great. The only change I made was to add the ‘w’ modifier to the open FilePath line as the file does not exist and permission errors result otherwise.

1 Like