How do i import CSV file into Table

Hello, i have tried to import a CSV file into the table but i have experienced ValueError.

This is the script on the button:
filepath= system.file.openFile(“csv”)
string= system.file.readFileAsString(filepath)
data = system.dataset.fromCSV(string)
even.source.parent.getComponent(‘Table’).data = data

This is the data i would like to import into the table
image

on the last line, the T in event is missing and ` (grave accent) instead of ’ (single quote)

filepath = system.file.openFile(“csv”)
string = system.file.readFileAsString(filepath)
data = system.dataset.fromCSV(string)
event.source.parent.getComponent(‘Table’).data = data

The system.dataset.fromCSV has to be in a special format and not from an excel save as CSV.
if you want to use the excel CSV format the link below page has an alternative script.
https://docs.inductiveautomation.com/display/DOC79/Exporting+and+Importing+a+CSV

Weird, I copied the code I posed from ignition to the website, then i copied it from the website back into ignition and it screws up the single quote and double quotes.

This worked for me.

# Import Python's built-in csv library.

import csv

# Ask the user to find the CSV in the local file system.

path = system. file .openFile( "csv" )

# Create a reader object that will iterate over the lines of a CSV.

# We're using Python's built-in open() function to open the file.

csvData = csv.reader( open (path))

# Create a List of strings to use as a header for the dataset. Note that the number

# of headers must match the number of columns in the CSV, otherwise an error will occur.

# The simplest approach would be to use next() to read the first line in the file, and

# store that at the header.

header = csvData. next ()

# Create a dataset with the header and the rest of our CSV.

dataset = system.dataset.toDataSet(header , list (csvData))

# Store it into the table.

event.source.parent.getComponent( 'Power Table' ).data = dataset

2 Likes

Yeah, you need to put code in a code formatting block. Basically, three backquotes by themselves on a line above and again on a line below your code. Like so:
```
pasted code
```

See this topic on discourse.org for more formatting options.

Thank you so much for helping. Appreciate it

Hello, I am using the script above.
My question is can I import the .csv file to the Table Row 1 instead of the header.
I am using the power table. The header data get replaced every time.
FYI, my csv file does not contain header name.
I do not want the data to overwrite my table header.

Crossposting is not allowed. Answer in the other thread.