我想创建一个文件一样 -列表使用Python和数据库
{
"name" : "John" ,
"DOB" : "21-Jun-1999" ,
"Sex" : "M" ,
"Skill" : ["C","PYTHON","COBOL"],
"Location" : { "lat":12.3 , "Long" : "14.6" }
}
我使用cx_oracle
拉数据从我的数据库中。 这里是我的代码的一小部分 -
cur1.execute('select name,dob,sex,skills,long,lat from employee');
# to create the keys
for i in range(len(cur1.description)):
head=cur1.description[i][0]
header = header + [head]
for row in cur1.fetchall():
data_dict={}
for i in range(len(row)):
if i == 3 :
c={}
c=row[i]
c=c.split(',')
for j in range(len(c)):
data_dict.setdefault(header[i],[]).append(c[j])
else:
data_dict.setdefault(header[i],[]).append(row[i])
print(data_dict)
这是输出 -
{
"name" : "John" ,
"DOB" : "21-Jun-1999" ,
"Sex" : "M" ,
"Skill" : ["C","PYTHON","COBOL"]
}
我如何转换的最后两个字段从我的SQL(长和LAT)并创建另一个列表。
令人惊叹!谢谢 – Judi
@Judi,我很高兴这有帮助。如果它回答你的问题,你能将这个答案标记为接受的答案吗?这有助于显示其他人搜索类似的问题。干杯。 – Clandestine