【发布时间】:2016-05-13 08:15:48
【问题描述】:
我希望创建一个类似 --
的文档{
"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 中的最后两个字段(long 和 lat)并创建另一个列表。
【问题讨论】: