Is there a way to convert a Dataset tag to a list of values on the Script Console?

I have a Memory tag named “Criticals”. It is a Dataset type. It only has 1 column and 4 rows of numbers.

I am trying to use the Script Console to generate a list variable of 4 numbers. My goal is to later use that list of numbers in a scripting.
I have tried various ways. Lastly, I tried something like this, but no success so far:

I haven’t looked at the rest of your code, but your first mistake is with:
Dataset = system.tag.read(“MX_Seating/R1Cx_01B/Criticals”).data

data is not an attribute of a tag read, use value:
Dataset = system.tag.read(“MX_Seating/R1Cx_01B/Criticals”).value

1 Like

Thank you,

I just found out that I can read from a Dataset tag this way:

print system.tag.read(“MX_Seating/R1Cx_01B/Criticals”).value.getValueAt(0, 0)
print system.tag.read(“MX_Seating/R1Cx_01B/Criticals”).value.getValueAt(1, 0)
print system.tag.read(“MX_Seating/R1Cx_01B/Criticals”).value.getValueAt(2, 0)
print system.tag.read(“MX_Seating/R1Cx_01B/Criticals”).value.getValueAt(3, 0)

However, now I am trying to figure out how can I loop only for the row values and not the column since column will stay as Column 0. Any suggestions?

tagValue = system.tag.read("MX_Seating/R1Cx_01B/Criticals").value
listOfRowValues = []
for row in range(tagValue.rowCount):
    listOfRowValues.append(tagValue.getValueAt(row, 0)
2 Likes

Thank you very much:

tagValue = system.tag.read("MX_Seating/R1Cx_01B/Criticals").value
listOfRowValues = []
for row in range(tagValue.rowCount):
    listOfRowValues.append(tagValue.getValueAt(row, 0))
    print tagValue.getValueAt(row,0)

You can also move it over to a pyDataSet and run through it that way.
I prefer to since then I don’t have to use the getValueAt

 listOfRowValues=[]	
      t = system.dataset.toPyDataSet(system.tag.read("MX_Seating/R1Cx_01B/Criticals").value)
 	     for row in t:
 	          listOfRowValues.append(row[0])
                  print row[0]
1 Like

+1 to what @MMaynard said. As an aside, the “pythonic way” (FWIW) would be to use a list comprehension:

t = system.dataset.toPyDataSet(system.tag.read("MX_Seating/R1Cx_01B/Criticals").value)
listOfRowValues = [row[0] for row in t]
print listOfRowValues

It’s also worth noting that the time may come when you have a two dimensional dataset to work with. In that instance, you might want to use a list of lists. The approach is very similar:

t = system.dataset.toPyDataSet(system.tag.read("MX_Seating/R1Cx_01B/Criticals").value)
# note the use of list() to create the entry
listOfRowValues = [list(row) for row in t]
print listOfRowValues
3 Likes

Thank you very much!

I appreciate your help. Many many Thanks!!

How would you return a straight list from a PyDataSet?

The most ‘pythonic’ way to get a doubly-nested list would probably be:
ll = [[l] for l in l]

Or less efficient but maybe less ‘magic’ (depending on who you’re talking to):
ll = list(map(lambda x: [x], l))

Either way, you could then run system.dataset.toPyDataset:
pds = system.dataset.toPyDataset(["values"], ll)

Note that datasets, and therefore PyDatasets, require ‘columns’ to have the same type, so [[o] for o in ['abc', 123]] will not be able to convert into a PyDataset.

Are you looking for a list of lists [[a,b,c], [d,e,f]] or a flat list [a,b,c,d,e,f] ?

I would be looking for a flat list.

WARNING: I typed this on my phone. :wink:
EDIT: Verified my snippet upon getting to my desk.

# Create PyDataSet
headers = ['col1', 'col2', 'col3']
data = [[1,2,3], [4,5,6], [7,8,9]]
ds = system.dataset.toPyDataSet(system.dataset.toDataSet(headers, data))

flatList =[]

for row in ds:
	flatList.extend(list(row))
	
print flatList

Or if we’re golfing, flatlist = [item for sublist in data for item in sublist] :slight_smile:

2 Likes

Huh. Millenial whippersnappers with cute new slang. No golf on my lawn!

3 Likes