# Converting table headers to PyDataSet

**URL:** <https://forum.inductiveautomation.com/t/converting-table-headers-to-pydataset/26234>\
**Category:** Ignition\
**Created:** [July 2, 2019, 5:18pm UTC](https://forum.inductiveautomation.com/t/converting-table-headers-to-pydataset/26234 "2019-07-02T17:18:28Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Peter.Dmitriev](https://avatars.discourse-cdn.com/v4/letter/p/7ea924/32.png) [@Peter.Dmitriev](https://forum.inductiveautomation.com/u/Peter.Dmitriev)\
**Post date:** [July 2, 2019, 5:18pm UTC](https://forum.inductiveautomation.com/t/converting-table-headers-to-pydataset/26234/1 "2019-07-02T17:18:28Z")

</div>

Hello,

I have a simple task of writing table column names to PLC tags but it doesn’t want to convert the list to PyDataSet.

Here is the code:  
headerVals = event.source.parent.getComponent(‘Power Table’).data.getColumnNames()  
headersToPLC = system.dataset.toPyDataSet(headerVals)

Here is the error message:  
TypeError: toPyDataSet(): 1st arg can’t be coerced to com.inductiveautomation.ignition.common.Dataset

It seems like I need to convert **java.util.Collections$UnmodifiableRandomAccessList** (that’s what _print type(headerVals)_ gives me) to Ignition dataset but I couldn’t figure out how to do it.

Any suggestions?

Thanks,  
Peter.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [July 2, 2019, 5:28pm UTC](https://forum.inductiveautomation.com/t/converting-table-headers-to-pydataset/26234/2 "2019-07-02T17:28:42Z")

</div>

PyDatasets are (basically) **2D** arrays, AKA lists _of lists_; the datastructure in pure Python would be something like:

```python
pds = [
    [col1, col2, col3], # row1
    [col1, col2, col3], # row2
    [col1, col2, col3], # row3
]

```

`getColumnNames()` returns a flat list:  
`[columnName1, columnName2, columnName2]`

Why are you trying to cast it into a pydataset? What’s the desired operation? You _may_ just need to put it inside of another list, but that’s skipping the actual question:

```python
headerVals = event.source.parent.getComponent(‘Power Table’).data.getColumnNames()
headersToPLC = system.dataset.toPyDataSet([headerVals])

```

---

<div class="post-metadata">

**Author:** ![Peter.Dmitriev](https://avatars.discourse-cdn.com/v4/letter/p/7ea924/32.png) [@Peter.Dmitriev](https://forum.inductiveautomation.com/u/Peter.Dmitriev)\
**Post date:** [July 2, 2019, 5:44pm UTC](https://forum.inductiveautomation.com/t/converting-table-headers-to-pydataset/26234/3 "2019-07-02T17:44:23Z")

</div>

@paul-griffith,

All I need is to write this data to PLC tags. I want to use system.opc.writeValues().

I’ve tried putting header list inside of another list with no luck - gives me the same error. Does it work for you? I’m running 7.9.10.

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [July 2, 2019, 5:57pm UTC](https://forum.inductiveautomation.com/t/converting-table-headers-to-pydataset/26234/4 "2019-07-02T17:57:23Z")

</div>

`system.opc.writeValues` expects a flat list of paths and values - something like:

```auto
paths = ["path1", "path2", "path3"]
values = event.source.parent.getComponent(‘Power Table’).data.getColumnNames()
system.opc.writeValues("Ignition OPC UA Server", paths, values)

```

No PyDatasets required - I’m not sure why you started down that path. Are you able to share a bit more of your script so I can see more of what you’re trying to do?

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [July 2, 2019, 5:58pm UTC](https://forum.inductiveautomation.com/t/converting-table-headers-to-pydataset/26234/5 "2019-07-02T17:58:56Z")

</div>

Or (pure speculation) are you trying to use the table headers to determine _which_ OPC tags to write to? And then extract values from somewhere else (the table data?) If so, this gets a bit more complicated, but still shouldn’t necessarily require PyDatasets.

---

<div class="post-metadata">

**Author:** ![Peter.Dmitriev](https://avatars.discourse-cdn.com/v4/letter/p/7ea924/32.png) [@Peter.Dmitriev](https://forum.inductiveautomation.com/u/Peter.Dmitriev)\
**Post date:** [July 2, 2019, 6:13pm UTC](https://forum.inductiveautomation.com/t/converting-table-headers-to-pydataset/26234/6 "2019-07-02T18:13:39Z")

</div>

Yes, that’s exactly where I started but it didn’t work for me:

```auto
basePath = 'ns=1;s=[' + event.source.parent.getComponent('PAC').text + ']' + event.source.parent.getComponent('TagName').text

opcuaServer = event.source.parent.getComponent('OPCUA').text

headerPaths = []
headerPaths.append(basePath + '.Headers[0]')
headerPaths.append(basePath + '.Headers[1]')
headerPaths.append(basePath + '.Headers[2]')
headerPaths.append(basePath + '.Headers[3]')
headerPaths.append(basePath + '.Headers[4]')
headerVals = event.source.parent.getComponent('Power Table').data.getColumnNames()
system.opc.writeValues(opcuaServer,headerPaths,headerVals)

```

Error:  
java.lang.ClassCastException: java.lang.ClassCastException: Cannot coerce value ‘[Meter Pressure, Meter K-factor, Meter F/V, Meter Frequency, Meter Density]’ into type: class [Ljava.lang.Object;

Now, here is part of the code that writes the actual table data to PLC tags. I could only make it work with Py datasets. System.opc.writeValues operates only on single row PyDataSets from what i can see:

```auto
intData = system.dataset.toPyDataSet(event.source.parent.getComponent('Power Table').data)

intPath = basePath + '.INT'
i=0
for i in range(0,20):
	intRowPaths = []
	intRowVals = []
	for k in range(0,5):
		intRowPaths.append(intPath + str(k+1) + '[' + str(i)+ ']')
		intRowVals.append(intData[i][k])
	
	system.opc.writeValues(opcuaServer,intRowPaths,intRowVals)

```

What am I missing something here?

Thanks for the help @paul-griffith

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [July 2, 2019, 6:18pm UTC](https://forum.inductiveautomation.com/t/converting-table-headers-to-pydataset/26234/7 "2019-07-02T18:18:06Z")

</div>

Try updating the first example to this:

```auto
basePath = 'ns=1;s=[' + event.source.parent.getComponent('PAC').text + ']' + event.source.parent.getComponent('TagName').text

opcuaServer = event.source.parent.getComponent('OPCUA').text

headerPaths = []
headerPaths.append(basePath + '.Headers[0]')
headerPaths.append(basePath + '.Headers[1]')
headerPaths.append(basePath + '.Headers[2]')
headerPaths.append(basePath + '.Headers[3]')
headerPaths.append(basePath + '.Headers[4]')
headerVals = list(event.source.parent.getComponent('Power Table').data.getColumnNames())
system.opc.writeValues(opcuaServer,headerPaths,headerVals)

```

---

<div class="post-metadata">

**Author:** ![Peter.Dmitriev](https://avatars.discourse-cdn.com/v4/letter/p/7ea924/32.png) [@Peter.Dmitriev](https://forum.inductiveautomation.com/u/Peter.Dmitriev)\
**Post date:** [July 2, 2019, 6:48pm UTC](https://forum.inductiveautomation.com/t/converting-table-headers-to-pydataset/26234/8 "2019-07-02T18:48:03Z")

</div>

That did the trick, Paul @paul-griffith

Thanks for your help!

---

<div class="post-metadata">

**Author:** ![paul-griffith](https://sea1.discourse-cdn.com/inductiveautomation/user_avatar/forum.inductiveautomation.com/paul-griffith/32/37249_2.png) [@paul-griffith](https://forum.inductiveautomation.com/u/paul-griffith)\
**Post date:** [July 2, 2019, 7:25pm UTC](https://forum.inductiveautomation.com/t/converting-table-headers-to-pydataset/26234/9 "2019-07-02T19:25:15Z")

</div>

For a little background - it looks like the `java.util.Collections$UnmodifiableRandomAccessList` that is returned from `getColumnNames` isn’t a close enough type to a regular Python list to be coerced by system.opc.writeValues - so, calling the `list()` function automatically creates a new, true `list` class instance based on the sequence of values provided - the `UnmodifiableRandomAccessList`.
