CSV Read Error - ValueError: CSV invalid format: expected #NAMES on line 1

Trying to read a CSV file to do some logic on it in the designer scripting console. I have this as my script -

csv_path = "C:\\Users\\bkarabinchak\\Desktop\\toRead.csv"
csv_string = system.file.readFileAsString(csv_path)
csv_file = system.dataset.fromCSV(csv_string)

and am getting the following error

Traceback (most recent call last):
  File "<input>", line 3, in <module>
ValueError: CSV invalid format: expected #NAMES on line 1

Here is my CSV file which seems very run of the mill so I am not sure what is going wrong - toRead.csv (2.0 KB)

How can I read this csv file?

Your CSV is not formatted correctly for system.dataset.fromCSV().

Here’s the format:
https://docs.inductiveautomation.com/display/DOC81/system.dataset.fromCSV

2 Likes

Boy do I have egg on my face telling my coworkers to read the documentation for problems and missing this one. Thanks.

2 Likes

Hello

I have tried to import CSV data into Ignition but it has problem with CSV file. I have checked example below and nothing.
https://docs.inductiveautomation.com/display/DOC81/system.dataset.fromCSV

I copied almost everything from both script and file content from the example mentioned before and it still says CSV is not formatted properly.

My script:

In this example it is assumed that the CSV file being read was a dataset

#that was previously exported using system.dataset.toCSV with the forExport flag set to True:

Specify file path.

file_path = "C:\VMs\alarms.csv"

Read in the file as a string.

data_string = system.file.readFileAsString(file_path)

Convert the string to a dataset and store in a variable.

data = system.dataset.fromCSV(data_string)

Assign the dataset to a table.

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

My CSV:

#NAMES
Col 1,Col 2,Col 3
#TYPES
I,str,D
#ROWS,6
44,Test Row 2,1.8713151369491254
86,Test Row 3,97.4913421614675
0,Test Row 8,20.39722542161364
78,Test Row 9,34.57127071614745
20,Test Row 10,76.41114659745085
21,Test Row 13,13.880548366871926

Result:

Traceback (most recent call last):
File "event:actionPerformed", line 8, in
ValueError: CSV invalid format: expected #NAMES on line 1

Ignition v8.1.26 (b2023032308)
Java: Azul Systems, Inc. 11.0.18

Can you help me?

This script worked perfectly for me on the test data using a button:

filePath = "C:/myPath/testFile.csv"
dataString = system.file.readFileAsString(filePath)
data = system.dataset.fromCSV(dataString)
event.source.parent.getComponent('Power Table').data = data

Result:
image

In case it helps, here is the test file I used:
testFile.csv (241 Bytes)