Random characters on the first header column of the CSV file import

I am trying to import a csv file to a table. I followed the documentation on Exporting and Importing a CSV - Ignition User Manual 8.1 - Ignition Documentation.

Upon trying the said example on “Calling the csv.reader Function” and used the exact script and csv contents I got random characters on my first header column:

Image 1

How can I fix this?

How closely did you follow the example? Those extra characters lead me to believe that the system.dataset.toCSV() function from the example was actually was executed with a forExport arg set to True. I suspect those characters are the information required in the system.dataset.fromCSV() function. Could you post your exact code in this thread?

I just copy and pasted this code from the documentation:

# 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

On a button press (Event Handler is actionPerformed) that script will execute.

Can you share the CSV file? I wonder if those characters are a UTF-8 BOM.

I overwritten the previous csv file but I make my csvs on excel and I save it as “CSV UTF-8”

users.csv (311 Bytes)

Is there something wrong with that format?

Excel saves that file type as UTF-8 BOM, so you’ll need to be sure to read that encoding.

import csv
import io
path = system.file.openFile("csv")
csvData = csv.reader(io.open(path, 'r', encoding='utf-8-sig'))

JordanCClark solution works. Another option if csv files is generated on excel is to save it as “Comma Delimited” format not “CSV UTF-8”.

1 Like