[SOLVED] Python dict problem

Hello,
I need to parse a python dictionnary coming from a table cell
here is the dict
texte = {'Mia':'Oli, c’est juste le bac,\on va pas le fêter tout l’été,\on s’détend…'}
If I use directly the dict, with
texteMia = texte['Mia']
I obtain
‘Oli, c’est juste le bac,\on va pas le fêter tout l’été,\on s’détend…’

If the dict is in a table cell and I use this script:

texte = op('textes')[1,'txt']
print(texte)
texteMia = texte['Mia']
print(texteMia)

I obtain that :frowning:

{'Mia':'Oli, c’est juste le bac,\on va pas le fêter tout l’été,\on s’détend…'}
Traceback (most recent call last):
  File "</project1/dictParse:op('/project1/dictParse').run()>", line 1
td.tdError: File "/project1/dictParse", line 3
TypeError: 'td.Cell' object is not subscriptable
Results of run operation resulted in exception.

If someone can say where is the problem, I would be very grateful!
Thanks in advance, Jacques

try to use

texte = op('textes')[1,'txt'].val

the hint is in the TypeError, it tells you you’re pointing to a cell instead of its contents

Hello,
Thanks, but that was tried with this result

{'Mia':'Oli, c’est juste le bac,\on va pas le fêter tout l’été,\on s’détend…'}
Traceback (most recent call last):
  File "</project1/dictParse:op('/project1/dictParse').run()>", line 1
td.tdError: File "/project1/dictParse", line 3
TypeError: string indices must be integers
Results of run operation resulted in exception.

Sorry. Can you post a small .toe with the DAT and your script? That would be easier to debug!

hello,
here is the .toe and the .tsv database
sqlite.zip (84.5 KB)
The final goal of the script is to parse each key (as ‘Mia’) in each line of remote iPhone
Thank you,
Jacques

Without looking at your file:

Well, in a table you always have strings. It may look like a dict, but it’s a string. hence you get the string indices error when you try to access it like a dict

So you could store your dicts as json strings inside DATs, and then grab it into your script , do a json.loads() to convert it to a dict …

1 Like

YES!
Thank you Achim for JSON, it works!
Thank you Nettoyeur for .val
here the working script:

import json
texte = op('table1')[1,'txt'].val
texteJS = json.loads(texte)
texteMia = texteJS['Mia']
1 Like