I am trying to do the same but only make a one column dataset. I was able to make a one row dataset set just fine, but for some reason Ignition give me a headers and column number are different error. Even through both the len() of my data set and headers are 1.
Here is the code with me trying to figure out how the headers and ata are different:
def runAction(self, event):
"""
Grabs one column from a CSV file and makes a dataset of that column and saves it to a tag.
Logger name = getTagAddDatasetAsColumn
"""
import csv
# variable
myLogger = system.util.getLogger("getTagAddDatasetAsColumn")
# Get our CSV path
filePath = self.getSibling("CSVFilePath").props.text.replace('\\','\\\\')
path = filePath.strip('"')
# build a column list to make a one column dataSet
datasetList = []
data =
# open the CSV file
with open(path, 'rU') as csvFile:
fileObject = csv.DictReader(csvFile,delimiter=',')
headerNameCounts = fileObject.fieldnames
# self.custom.key = headerNameCounts
headerName = str(fileObject.fieldnames[0])
# self.custom.key = headerName
if len(headerNameCounts) == 1:
for row in fileObject:
# self.custom.key = row[headerName]
datasetList.append(row[headerName])
data = [datasetList] # must be a list of rows
headers = [headerName] # needs to be a list of strings
# buildDataset = system.dataset.toDataSet(headers, data)
system.perspective.print("Len of our headers: {}".format(len(headers)))
system.perspective.print("headers output: {}".format(headers))
system.perspective.print("Len of our dataSet {}:".format(len(data)))
system.perspective.print("Data output: {}".format(data))
# system.perspective.print(buildDataset)
else:
myLogger.info("You have to many columns for this function call. Only one column is needed.")
Here is the out put. Its just UDT parameters' in a one column CSV file:
12:55:04.551 [Browser Thread: d20c45f7-fdf4-46ba-97a4-f6b4956f33ea] INFO Perspective.Designer.Workspace - Len of our headers: 1
12:55:04.551 [Browser Thread: d20c45f7-fdf4-46ba-97a4-f6b4956f33ea] INFO Perspective.Designer.Workspace - headers output: ['UDT Parameters']
12:55:04.551 [Browser Thread: d20c45f7-fdf4-46ba-97a4-f6b4956f33ea] INFO Perspective.Designer.Workspace - Len of our dataSet 1:
12:55:04.551 [Browser Thread: d20c45f7-fdf4-46ba-97a4-f6b4956f33ea] INFO Perspective.Designer.Workspace - Data output: [['AlarmRollupTag', 'AssociatedAnalyzer', 'AssociatedL3', 'Description', 'DeviceAddress', 'Disposition', 'DOT', 'EquipmentID', 'EquipSubType', 'EquipType', 'Facility', 'FacilityID', 'FilterID_01', 'FilterID_02', 'FilterID_03', 'ForemanArea', 'Latitude', 'Longitude', 'MDMKey', 'MeasurementArea', 'MeasurementType', 'MeterID', 'OrderAndType', 'PressureType', 'Producer', 'Route', 'RTU_Instance', 'Safety', 'SAP_ParentEquipment', 'SAPID', 'Service', 'Site', 'SpanOfControl', 'State', 'Topic', 'TotalInterlocks']]
I have no clue why this is giving me this error once I uncomment the toDataset part.
error:
com.inductiveautomation.ignition.common.script.JythonExecException
Traceback (most recent call last):
File "<function:runAction>", line 29, in runAction
IndexError: Row 0 doesn't have the same number of columns as header list.
at org.python.core.PyException.doRaise(PyException.java:211)
at org.python.core.Py.makeException(Py.java:1654)
at org.python.core.Py.makeException(Py.java:1658)
at org.python.core.Py.makeException(Py.java:1662)
at org.python.core.Py.makeException(Py.java:1666)
at org.python.pycode._pyx3318.runAction$1(<function:runAction>:41)
at org.python.pycode._pyx3318.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:868)
at com.inductiveautomation.ignition.common.script.ScriptManager$ScriptFunctionImpl.invoke(ScriptManager.java:1010)
at com.inductiveautomation.ignition.gateway.project.ProjectScriptLifecycle$AutoRecompilingScriptFunction.invoke(ProjectScriptLifecycle.java:950)
at com.inductiveautomation.perspective.gateway.script.ScriptFunctionHelper.invoke(ScriptFunctionHelper.java:161)
at com.inductiveautomation.perspective.gateway.script.ScriptFunctionHelper.invoke(ScriptFunctionHelper.java:98)
at com.inductiveautomation.perspective.gateway.action.ScriptAction.runAction(ScriptAction.java:80)
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)
Caused by: org.python.core.PyException
Traceback (most recent call last):
File "<function:runAction>", line 29, in runAction
IndexError: Row 0 doesn't have the same number of columns as header list.
... 31 more
Ignition v8.1.36 (b2024010211)
Java: Azul Systems, Inc. 17.0.8