This lesson is part of the Scripting in Ignition course. You can browse the rest of the lessons below.

Supplemental Videos

Description

Learn how to export a dataset from a query or table to a CSV file.

Video recorded using: Ignition 8.1

Transcript

(open in window)

[00:00] In this lesson, we'll look at how to export a data set to a CSV, a comma-separated values file. This can be very useful for offline processing or visualization of data and fortunately, it doesn't take too much scripting to do, just a couple of lines. So here in the designer, we've got a table with some random data and a button which will trigger the CSV write using an event-handler script. As we can see by scrolling down in the property editor for the table, the data in this table is indeed the dataset with the indicated 150 rows by three column size. So, let's open up the script and let's take a look at it. For starters on line two, we'll extract the data from the table component into a dataset where event.source is the button itself. Dot parent is its root container. Get component leads us back down to the table component and dot data is the desire data itself and by the way, this example would work equally well for any data set that came from anywhere, data sets or data sets, whatever their source, be it a table like here, the system.db.run query database query, or perhaps some component parameters.

[01:18] Then let's look at these next three simple statements using various system functions. Line five converts the dataset into a multiline string representing the CSV values using dataset dot two CSV. Line eight prompts for the user for a desired file name with export one dot CSV as an initial suggestion using file dot save file. Then assuming the user didn't hit cancel, which is checked here on line 11. Finally, line 12 writes the CSV data into the specified file name path using file dot right file. Note that each of the previous three functions have other options but we're just using the most simple function forms which do what we need.

[02:04] If you wanna see those other options, just see the system functions API in the user manual appendix. Okay, let's run this in preview mode and let's try this out. Here's the file prompt dialogue with the initial export one dot CSV file name suggestion going to my downloads directory and we'll hit save then open up my minimize downloads folder and there is the file. If we open it up, we'll see it's the same table data in multiline CSV format as expected. And we can see that the first three lines of the table match up exactly to the first three lines here. Well, now that we've seen how to write a data set to a CSV file and a couple of simple steps, it turns out we can even do it all in one step. So let's bring back the script editor from the task bar, and then we will uncomment this last line using control slash and then we will comment out these earlier statements leaving the first line enabled of course, like so, and we'll apply all the changes.

[03:07] So we can do the same thing in one statement, using data set dot export CSV by providing three inputs. Again, a suggested file name. And this time I had a row flag saying, yeah, we want headers in the file and the data set to export. So if we run the CSV script again and we'll write to a different file export two this time and save it and then open up the downloads folder, we see the second file and opening it up, we see all the same CSV data. All done in one step this time. So to wrap up. In this lesson, we've seen how to export an entire ignition dataset to a CSV file using a script with as little as two lines of Python.

You are editing this transcript.

Make any corrections to improve this transcript. We'll review any changes before posting them.