CSV import - Cannot create PyString with non-byte value

on File Upload
Script

def runAction(self, event):
	name = event.file.name.split(".")[0]
	from java.lang import String
	import csv
	dataAsString = String(event.file.bytes, "utf-8")
	data = csv.reader(dataAsString.split("\r\n"))
	data_list = []
	for row in data:
		data_list.append(row)

This File does not work
image

This File works without error
image

error is

om.inductiveautomation.ignition.common.script.JythonExecException
Traceback (most recent call last):
  File "<function:runAction>", line 12, in runAction
java.lang.IllegalArgumentException: Cannot create PyString with non-byte value

java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Cannot create PyString with non-byte value

	caused by org.python.core.PyException
Traceback (most recent call last):
  File "<function:runAction>", line 12, in runAction
java.lang.IllegalArgumentException: Cannot create PyString with non-byte value

java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Cannot create PyString with non-byte value

	caused by IllegalArgumentException: Cannot create PyString with non-byte value

Ignition v8.1.33 (b2023101913)
Java: Azul Systems, Inc. 17.0.8

Error running action 'component.onFileReceived' on Docked/Tools TGMS@D/root/CoordinateContainer/FileUpload: Traceback (most recent call last): File "function:runAction", line 12, in runAction java.lang.IllegalArgumentException: Cannot create PyString with non-byte value 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.pycode.pyx1712.runAction$1(function:runAction:21) at org.python.pycode.pyx1712.call_function(function:runAction) at org.python.core.PyTableCode.call(PyTableCode.java:173) at org.python.core.PyBaseCode.call(PyBaseCode.java:306) at org.python.core.PyFunction.function___call(PyFunction.java:474) at org.python.core.PyFunction.call(PyFunction.java:469) at org.python.core.PyFunction.call(PyFunction.java:464) at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:847) at com.inductiveautomation.ignition.common.script.ScriptManager.runFunction(ScriptManager.java:829) at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$TrackingProjectScriptManager.runFunction(ProjectScriptLifecycle.java:852) at com.inductiveautomation.ignition.common.script.ScriptManager$ScriptFunctionImpl.invoke(ScriptManager.java:1010) at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$AutoRecompilingScriptFunction.invoke(ProjectScriptLifecycle.java:934) at com.inductiveautomation.perspective.gateway.script.ScriptFunctionHelper.invoke(ScriptFunctionHelper.java:158) at com.inductiveautomation.perspective.gateway.script.ScriptFunctionHelper.invoke(ScriptFunctionHelper.java:97) at com.inductiveautomation.perspective.gateway.action.ScriptAction.runAction(ScriptAction.java:74) at com.inductiveautomation.perspective.gateway.model.ActionCollection$ActionSequence$ExecuteActionsTask.lambda$call$0(ActionCollection.java:263) at com.inductiveautomation.perspective.gateway.api.LoggingContext.mdc(LoggingContext.java:54) at com.inductiveautomation.perspective.gateway.model.ActionCollection$ActionSequence$ExecuteActionsTask.call(ActionCollection.java:252) at com.inductiveautomation.perspective.gateway.model.ActionCollection$ActionSequence$ExecuteActionsTask.call(ActionCollection.java:221) at com.inductiveautomation.perspective.gateway.threading.BlockingTaskQueue$TaskWrapper.run(BlockingTaskQueue.java:154) at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Unknown Source) at java.base/java.util.concurrent.FutureTask.run(Unknown Source) at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at com.inductiveautomation.perspective.gateway.threading.BlockingWork$BlockingWorkRunnable.run(BlockingWork.java:58) at java.base/java.lang.Thread.run(Unknown Source) java.lang.IllegalArgumentException: java.lang.IllegalArgumentException: Cannot create PyString with non-byte value

is where it is pointing for the error.

The error's in the Jython stdlib CSV module.

This might work?

def runAction(self, event):
	name = event.file.name.split(".")[0]
	from java.lang import String
	import csv
	dataAsString = String(event.file.bytes, "utf-8")
	data = csv.reader(map(unicode, dataAsString.split("\r\n")))
	data_list = []
	for row in data:
		data_list.append(row)

appears to be same result.

Odd thing to me is that it seems to work with one file and not another and I can't seem to find anything materially different.

Top one works... bottom does not


if there is another way to convert csv file to list of list in python that would work as well, just this is the method I found.

Can you upload the exact files you're testing with here?

1 Like

Inputs5.csv (51 Bytes)
Sectors.csv (1.6 KB)

Inputs5.csv has a BOM on it. Try using the utf-8-sig codec.

3 Likes

What is the filename of "top one" and "bottom one"?

Three of the files are processed with Loadsheet.UpdateLocData.<filename> while Inputs.csv is processed with Loadsheet.Input.createTags. Is it createTags that's causing the problem?

@JordanCClark is probably correct. If you check the encoding menu in Notepad++ for each file you can see the difference in encoding (but not in the content!).

ok looked that up and saved it as UTF-8 without the BOM and that worked. Now to figure out how it got there since I created the files the same way.

This codec should ignore the BOM if it is there.

1 Like