UnboundLocalError:赋值前引用的局部变量,未引用

时间:2017-11-20 19:09:02

标签: python

def foo() :
     for i in range(0, 5):

            path2 = "data." + str(i) + "\\log." + str(i)
            file = path + path2

            try:
                if  os.path.getsize(file) > 0:
                    print("All good")
                    f=open(file)
                    string=f.read()
                    pattern="(cd\s/[a-zA-Z0-9]*.*)"
                    matches=re.findall(pattern,string)
                    for input in matches:
                        print(input)
                        a=input.split(";")
                        x=len(a)
                        print(x)
                        cmd.append(x)

                        y=0
                        if y <= x:
                            y=y+1
                            print(a)
                            listofcmd.append(a)
                            continue

                print(cmd)
                print(listofcmd)
                dictionary=dict(zip(cmd,listofcmd))
                print("Number of commands : List of Commands")
                print(dictionary)
                for k,v in dictionary.items():
                    print(k,v)
                row=c.fetchall() 
                c.execute('''INSERT INTO log (Numberofcmds, Listofcmds)
                                         VALUES (?, ?)''', (k,v))
                conn.commit()
                sqlstr1=('SELECT (Numberofcmds, Listofcmds) FROM log ')
                for row in c.execute(sqlstr1):
                    print(str(row[0]), row[1])




            except OSError:  
                 print("File not found")    

     i=i+1
     return i

foo()           

我想将数据插入到数据库中,并且我得到了一个未在循环外引用的变量“k”的unboundlocalerror。这可能是什么问题? 我试图插入会话ID和时间同样我没有遇到任何问题,但现在当我试图添加这些数据我不断得到这个错误我有多少改变。   错误: VALUES(?,?)''',(k,v)) UnboundLocalError:在赋值之前引用的局部变量'k'

0 个答案:

没有答案