Dadep Dadep - 14 days ago 6
Python Question

PyQt : make a Dict from Text Edit, or transform a PyQt4.QtCore.QStringList in common Dict

I get from QtextEdit some "code", I mean something like :

1, A, B, D, 1
1, B, B, D, 2
2, A, C, G, 1


and I would like to get a dictionary like :

table={'1':{'A':['B', 'D', '1'], 'B':[B, D, 2]}, '2':{A:['C', 'G', '1']}


And then access it:
Var=table[1][A][B]


but my table is :
PyQt4.QtCore.QStringList
object at
0xb50094c4


and I get error :

File "main.py", line 45, in initMachine
self.TM.execute_TM(self.a, c, etat1)
File "/home/unifesp/Qt_Applications/Ruban.py", line66, in execute_TM
nvSymb=self.table[etatAct][symb][0]
TypeError: string indices must be integers


The problem come from
QStringList
format ?

def initMachine(self):
etat1=self.etat_1.text()
b = self.Table.toPlainText()
c=self.TM.obtenir_table(b)
print c

def obtenir_table(self, code):
code=str(code)
self.a=code.split("\n")
table={}
for i in range(len(self.a)):
b=self.a[i].split(', ')

etat=b[0]
symbole=b[1]
if etat=='':
print "ligne %i" %(i+1) + " : erreur "
if not table.has_key(etat):
table[etat] = {}
if not table[etat].has_key(symbole):
table[etat][symbole]=b[2:]
else:
print "ligne %i" %(i+1) +" : already declare"
self.table=self.convert_dict(table)

def convert_dict(self, dictionary):
if not isinstance(dictionary, dict):
return dictionary
return dict((str(k), self.convert_dict(v))
for k, v in dictionary.items())
def execute_TM(self, table, ruban, etat1):
self.Ruban.position=1
self.table=unicode(table)
etatAct=str(etat1)
while etatAct != 'stop':
symb=self.Ruban.lire_cellules()
print symb
print table
nvSymb=self.table[etatAct][symb][0]
self.Ruban.ecrire(nvSymb)
if table[etatAct][symb][1]=='D':
self.Ruban.deplacement_droite()
if table[etatAct][symb][1]=='G':
self.Ruban.deplacement_gauche()
else:
print "erreur code deplacement"
etatAct=table[etatAct][symb][2]


I have tried many different things, here the last version of what I have tried....

Thanks for your Help!

Answer

I found the problem came from the parameters I gave to execute the function execute_TM, table was not a dict.

the input was wrong

Merci Jean-Fran├žois !