PyQt:从文本编辑中生成Dict,或者转换PyQt4。QtCore.QStringList公共Di

2024-10-01 07:42:31 发布

您现在位置:Python中文网/ 问答频道 /正文

我从QtextEdit得到一些“代码”,我的意思是:

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

我想买一本字典:

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

然后访问它:Var=table[1][A][B]

但我的表是:PyQt4.QtCore.QStringList对象位于0xb50094c4

我得到了错误:

^{pr2}$

问题来自QStringList格式?在

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]

我试过很多不同的东西,这里是我尝试过的最后一个版本。。。。在

谢谢你的帮助!在


Tags: selfdictionaryifdeftablecodedictprint