Csv file import - Error: Cannot create PyString with non-byte value

Hi I’m trying to import a csv file to database via project scripting.
I’m able to import 3 files. But on one file i get this error

Error running action ‘component.onActionPerformed’ on Page/LOGSHEET/T2OPERATION@D/root/TabContainer/FCdata/TCdataentry/FCproduction/FCmain/FCbalance/FCcommand/Button: Traceback (most recent call last): File " ", line 6, in runAction File " ", line 120, in t2axjobimport File “C:\Program Files\Inductive Automation\Ignition\user-lib\pylib\csv.py”, line 104, in next row = self.reader.next() at org.python.core.PyString. (PyString.java:57) at org.python.core.PyString. (PyString.java:70) at org.python.core.PyString. (PyString.java:74) at org.python.modules._csv.PyReader.parse_save_field(PyReader.java:249) at org.python.modules._csv.PyReader.parse_process_char(PyReader.java:165) at org.python.modules._csv.PyReader.iternext(PyReader.java:92) at org.python.core.PyIterator.next(PyIterator.java:45) at jdk.internal.reflect.GeneratedMethodAccessor428.invoke(Unknown Source) at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.base/java.lang.reflect.Method.invoke(Unknown Source) java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Cannot create PyString with non-byte value

May I know what is the root cause? Something to do with csv file or coding.

data_string = system.file.readFileAsString(path)
reader = csv.DictReader(StringIO(data_string))

Looks like it’s a Jython error (IllegalArgumentException: Cannot create PyString with non-byte value · Issue #90 · jython/frozen-mirror · GitHub), but I have some ideas for things we can try.

What version of Ignition are you on? If you’re willing to DM me the csv file, I’d be glad to poke at it and see what’s going on.

I was able to get a sample file (encoded in UTF-8) containing Chinese characters to decode in csv.DictReader with a little finagling:

import csv
from io import StringIO
from org.python.core.util import StringUtil

path = r"C:\Users\pgriffith\Downloads\IM Translation_Small.csv"

data = system.file.readFileAsBytes(path)
reader = csv.DictReader(StringIO(StringUtil.fromBytes(data)))

for row in reader:
	print {key: value.decode("utf-8") for key, value in row.items()}
2 Likes

Thanks @KathyApplebaum @PGriffith for prompt response.
Unicode on my csv file is on this text “Disposal of Oily Water 10AD-01”