如何使用Python将字典转换并插入MYSQL插入

时间:2019-05-17 07:45:31

标签: python mysql dictionary-comprehension

我想将数据字典保存到mysql数据库中 我有一个var'dictOFTF_IDF'包含这样的字典

{0: [('komisi', 0.24323876863191415), ('yudisial', 0.24323876863191415), ('universitas', 0.04205902957765161), ('jalin',0.24323876863191415), ('kerjasama', 0.24323876863191415), ('berantas', 0.24323876863191415), ('mafia', 0.24323876863191415), ('adil', 0.24323876863191415)], 1: [('sar', 0.17690092264139212), ('trunojoyo', 0.014013698166114396), ('diklat', 0.17690092264139212), ('bumi', 0.17690092264139212), ......]} 

问题是这样的

columns = data.keys()
AttributeError: 'int' object has no attribute 'keys'

我正在这样做:

for data in dictOFTF_IDF:
    columns = data.keys()
    values = data.values()
    sql = "INSERT INTO %s (%s) VALUES(%s);" %('preprocessing_tb', columns, values)
    mycursor.execute(sql, dictOFTF_IDF)
    mydb.commit()

我希望结果可以存储在预处理表tb

0 个答案:

没有答案