Problem with a list

Hello,
I have a problem with a list written in a DAT.
If I write a list in a DAT cell, when I read it, I cannot extract an item in the list
here is the script:

liste = [0,13,15,23]
print (liste[2])
op(‘table1’)[0,0] = liste
liste2 = op(‘table1’)[0,0]
print(liste2)
print(liste2[2])

and the answer

python >>>
15
[0, 13, 15, 23]
Traceback (most recent call last):
File “</project1/text1:op(’/project1/text1’).run()>”, line 1
td.Error: File “/project1/text1”, line 6
TypeError: ‘td.Cell’ object is not subscriptable
Results of run operation resulted in exception.

I need to be able to read it as a list to parse it on another tables.
Someone have a solution? Its 4 hours I am blocked on that, searching every possible way. I tried making a dictionary saved in operator with the same problem.
Thank you in advance!
Jacques

listeDat.toe (3.4 KB)

The contents of a cell are a string to TouchDesigner. You can convert this back to a list by using the .split() python method.

We do have to do a little extra work though - we first cast your cell as a string:

op('table1')[0,0].val

Then we need to remove the brackets:
op('table1')[0,0].val[1:-1]

Then we need to split the contents of the string everytime there is a comma and a space , :
op('table1')[0,0].val[1:-1].split(', ')

This will give you a list of strings:

If you’d like this to be a list of integers you can convert with a list comprehension:
[int(val) for val in op('table1')[0,0].val[1:-1].split(', ')]

Similarly if you want them to be floats you can do the same:
[float(val) for val in op('table1')[0,0].val[1:-1].split(', ')]

If you have control over the table, you can do comma separated values, and then us a convert DAT to turn this into columns:

The row() method for table DAT’s will give you a list of columns when you ask for a specific row:

op('convert1').row(1)

These will be cell objects, so you need these to be specific data types, you’ll need a list comprehension:

[int(val) for val in op('convert1').row(1)]

Matthew’s is a very nice Touch-centric way of doing it.

I am more of a brute-force Python guy, so just use this:

liste2 = eval(op(‘table1’)[0,0].val)

This converts the cell data into a Python object.

(EDIT: added “.val” to above code)

3 Likes

Thank you everybody. Ivan, your solution doesn’t work: TypeError: eval () arg 1 must be a string, bytes or code object.
Matthew, I found a similar solution during that time:

liste = [0,13,155,23]
print (liste[2])
op('table1')[0,0] = liste
liste2 = op('table1')[0,0]
liste2 = liste2.val
liste2 = liste2.split()
l = len(liste2)
liste3 = []
for i in range (0,l):
	item = liste2[i]
	if i == 0:
		item = item[1:]
	else:
		item = item[0:]
	item = item [:-1]
	liste3.append(item)
print (int(liste3[2]))

listeDat.toe (3.6 KB)
I still hope for a more elegant solution…

Sorry added edit to my above comment. Will greatly simplify your code.

1 Like

Bravo Ivan!
Very elegant, one line and it works.

Thank you
Ivan DelSol
, I must give some explanation

We tag a bunch of video clip (and sub clip) in FinalCut Pro, exporting as XML

I import and parse the XML in TD, obtaining two list:

– the list of clips, name of the file, TC in and TC out, attached keywords

– the list of keywords with, for each clip, the list of attached clip. I have tried to put each clip number in different column but the task of retrieving it block the movie play. With list I can randomise it and parse it easily.

Ivan technic works with DAT but also with python dictionaries, opening great expectations.

This is exactly what I was looking for, thanks!