我是python的新手,在尝试将数据从嵌套字典插入MySQLdb时遇到了问题。我有一个字典,看起来像这样,但更长,可以变化的长度。
d = {
'Object_a': {
'parameter_1': {
'Cost': 12.00,
'Markup': 23.4555
},
'parameter_2': {
'Cost': 45.22,
'Markup': 11.222,
'Height': 44.33
}
},
'Object_b': {
'parameter_3': {
'Length': 12.00,
'Width': 23.4555
},
'parameter_1': {
'Cost': 1.12,
'Area': 4,
'Volume': 16.72
}
}
}
我一直在寻找一种方法来将嵌套字典操作到下面的表单中。 (我不知道如何做一张桌子,所以它采用csv风格,对不起)
Object, Parameter, Cost, Markup, Height, Length, Width, Area, Volume
Object_a, parameter_1, 12.00, 23.4555, , , , , ,
Object_a, parameter_2, 45.22, 11.222, 44.33, , , , ,
Object_b, parameter_3, , , , 12.00, 23.4555, ,
Object_b, parameter_2, 1.12, , , , , 4, 16.72
将数据输入SQL数据库的My Code如下所示:
目前,它需要在程序的另一部分中为其提供数据库名称,表名和字典。目前,这将检查是否创建了一个表,如果没有创建一个表,如果创建了一个表,它将检查哪些列存在,如果有新列,它将添加它们并将值插入新列和现有列。
我并不担心这些代码,但我主要担心的是如何将一个字典传递给这个函数以获得所需的形式,或者如何遍历上面的嵌套字典。
def tablecreate(cursor, tablename, dict):
# Creates a list of keys to use as column names
cols = list(dict.keys())
# If warning occurs, no table exists so create one
try:
sqlcheck = "SELECT 1 FROM {} LIMIT 1".format(tablename)
cursor.execute(sqlcheck)
except:
sql = "CREATE TABLE IF NOT EXISTS %s (ID INT AUTO_INCREMENT, PRIMARY
KEY(ID))" %(tablename)
cursor.execute(sql)
# Creating the rest of the columns
for i in range(0, len(cols)):
sql = "ALTER TABLE %s ADD COLUMN %s VARCHAR (50)" % (tablename,
cols[i])
cursor.execute(sql)
#Creates a list of new columns to be inserted into Table
sql = "SELECT COLUMN_NAME FROM information_schema.columns WHERE
TABLE_NAME ='%s'" % (tablename)
cursor.execute(sql)
a = cursor.fetchall()
b=[element for tupl in a for element in tupl]
new_cols = [x for x in cols if x not in b]
print (new_cols)
for i in range(0, len(new_cols)):
sql = "ALTER TABLE %s ADD COLUMN %s VARCHAR (50)" % (tablename,
new_cols[i])
cursor.execute(sql)
# Inserting values into the created columns
placeholders = ', '.join(['%s'] *len(dict))
columns = ', '.join(dict.keys())
sql = "INSERT INTO %s (%s) VALUES (%s)" % (tablename, columns,
placeholders)
cursor.execute(sql, dict.values())
答案 0 :(得分:1)
从你的问题中不清楚你想要数据的形式,但是对于操纵数据,pandas
在这方面非常擅长:
import pandas as pd
objs = []
for obj in d:
for param in d[obj]:
x = {
'Object': obj,
'Parameter': param,
}
x.update(d[obj][param])
objs.append(x)
df = pd.DataFrame(objs).fillna('').set_index(['Object', 'Parameter'])
print(df)
print(df.to_csv())
d = {
'Object_a': {
'parameter_1': {
'Cost': 12.00,
'Markup': 23.4555
},
'parameter_2': {
'Cost': 45.22,
'Markup': 11.222,
'Height': 44.33
}
},
'Object_b': {
'parameter_3': {
'Length': 12.00,
'Width': 23.4555
},
'parameter_1': {
'Cost': 1.12,
'Area': 4,
'Volume': 16.72
}
}
}
Area Cost Height Length Markup Volume Width
Object Parameter
Object_a parameter_2 45.22 44.33 11.222
parameter_1 12 23.4555
Object_b parameter_3 12 23.4555
parameter_1 4 1.12 16.72
Object,Parameter,Area,Cost,Height,Length,Markup,Volume,Width
Object_a,parameter_2,,45.22,44.33,,11.222,,
Object_a,parameter_1,,12.0,,,23.4555,,
Object_b,parameter_3,,,,12.0,,,23.4555
Object_b,parameter_1,4.0,1.12,,,,16.72,