json.loads(string)的结果仍然是一个字符串

时间:2017-07-05 07:57:32

标签: python json python-3.x sqlite

我目前正在尝试使用Python 3.6(在Spyder下)以及更准确地说是sqlite3(3.14.2)来构建数据库。由于最后一个模块不是为了在数据库中启用列表插入而设计的,我使用json(2.0.9)模块将我的列表转换为字符串。以下函数用于将数据从数据库传输到另一个,以及我第一次创建和插入列表的位置,这给我带来了奇怪的结果:

def database_concatenation():
    global log,name2,name3,timefin,path
    conn = sqlite3.connect(path+name2)
    cursor = conn.cursor()
    conn2 = sqlite3.connect(path+name3)
    cursor2 = conn2.cursor()
    cursor.execute("""select sqlite_version()""")
    print(cursor.fetchall())
    cursor.execute("""SELECT * FROM {}""".format(name2))
    for dataline in cursor.fetchall():
        cursor2.execute("""
                       SELECT * FROM {} 
                   WHERE (
                   timestp = ?
                   AND idq = ?
                   AND idz = ?
                   AND portq = ?
                   AND portz = ?
                   AND prot = ?);
                   """.format(name3) , (dataline[0],dataline[1],dataline[2],dataline[3],dataline[4],dataline[5]))
        result = cursor2.fetchone()

        if result == None:
            with open(path+log,"a") as login:

                login.write(timefin + " Warning ! Unusual connection :" + str(dataline)+ '\n')


            cursor2.execute("""
                       INSERT INTO {}(timestp,
                       idq,
                       idz,
                       portq,
                       portz,
                       prot,
                       Leftmean,
                       Leftstdev,
                       Leftmean10,
                       Leftstdev10,
                       Rightmean,
                       Rightstdev,
                       Rightmean10,
                       Rightstdev10,
                       nbdays) VALUES(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?);""".format(name3), (dataline[0],dataline[1],dataline[2],dataline[3],dataline[4],dataline[5],dataline[6],0,json.dumps([dataline[6]]),0,dataline[7],0,json.dumps([dataline[7]]),0,1))

        else:
            print(result[8])
            Leftmean10 = json.loads(result[8])
            print(Leftmean10)
            Rightmean10 = json.loads(result[12])
            print(type(Leftmean10))

            nbdays = result[14]
            aL,bL = intervalle_confiance(result[6],result[7],result[14])
            aL10,bL10 = intervalle_confiancebis(Leftmean10,result[9],min(nbdays,10))
            aR,bR = intervalle_confiance(result[10],result[11],result[14])
            aR10,bR10 = intervalle_confiancebis(Rightmean10,result[13],min(nbdays,10))
            rhwmny = dataline[7]
            lhwmny = dataline[6]





        if nbdays >= 10:
            Leftmean10.pop(0)
            Rightmean10.pop(0)
            Leftmean10.append(dataline[6])
            Rightmean10.append(dataline[7])
            Lmean = result[6]
            Lmean = Lmean*(nbdays/(nbdays+1)) + dataline[6]/(nbdays)
            Rmean = result[10]
            Rmean = Rmean*(nbdays/(nbdays+1)) + dataline[7]/(nbdays)
            Lstdev,Rstdev = result[7],result[11]



            add = (dataline[6] - Lmean)
            Lstdev = sqrt(Lstdev*Lstdev + (add*add))
            add = (dataline[7] - Rmean)
            Rstdev = sqrt(Rstdev*Rstdev + (add*add))
            mean = (sum(Leftmean10)/min(10,nbdays))
            meansquare = mean*mean

            othermean = (sum([i*i for i in Leftmean10])/min(10,nbdays))
            Lstdev10 = othermean - meansquare


            mean = (sum([int(i) for i in Rightmean10])/min(10,nbdays))
            meansquare = mean*mean

            othermean = (sum([i*i for i in Rightmean10])/min(10,nbdays))
            Rstdev10 = othermean - meansquare

            nbdays +=1

            Leftmean10 = json.dumps(result[8])
            Rightmean10 = json.dumps(result[12])
            print(Leftmean10)

            cursor2.execute("""UPDATE {} SET Leftmean = ?,
                   Leftstdev = ?,
                   Leftmean10 = ?,
                   Leftstdev10 = ?,
                   Rightmean = ?,
                   Rightstdev = ?,
                   Rightmean10 = ?,
                   Rightstdev10 = ?,
                   nbdays = ?  WHERE (timestp = ? AND idq = ?
                   AND idz = ?
                   AND portq = ?
                   AND portz = ?
                   AND prot = ?);""".format(name3), (Lmean,Lstdev,Leftmean10,Lstdev10,Rmean,Rstdev,Rightmean10,Rstdev10,nbdays,result[0],result[1],result[2],result[3],result[4],result[5]))




    conn.commit()
    conn2.commit()
    conn.close()
    conn2.close()
    bla= "it works"

    return(bla)    

当我第一次启动它时(基础“name3”已创建但为空),始终验证条件“if result = None”,并正确填充数据库。特别是,使用函数json.dumps(mylist)成功存储了我的列表 第二次处理时会出现问题:块

print(result[8])
Leftmean10 = json.loads(result[8])
print(Leftmean10)
Rightmean10 = json.loads(result[12])
print(type(Leftmean10))

返回

"[69]"
[69]
<class 'str'>

由于此'str'类型,我还会得到以后的错误。

我不明白发生了什么。 json.loads函数应该返回一个列表,不是吗?这就是它似乎做的事情:print(Leftmean10)返回我[69]而不是"[69]",除了..这是程序仍然是一个字符串^^

提前感谢您的回答!

1 个答案:

答案 0 :(得分:0)

您的字符串result[8]实际上包含双引号字符:

>>> print('[69]')
[69]
>>> print('"[69]"')
"[69]"

json.loads内,这已正确转换为字符串"[69]"。解决这个问题的一种方法是删除这两个双引号:

json.loads(result[8][1:-1])