我有一个json文件,我将它用作Python中的字典。json文件非常大。我正试图编写一个python代码,通过在表模式之前添加一个"source."来更新每个"查询"。然后将更新后的字典用于其他编程目的。
SQL脚本可以有联接、笛卡尔联接、子查询等。
预期输出:
"query": "SELECT a.column1, b.column2
FROM source.abcd.hist a, source.efgh.present b
WHERE (select column3, column4 from UPS where a.id = b.id )"
"query": "SELECT a.column1, b.column2
FROM source.apple.hist a, source.mango.present b
WHERE (select column3, column4 from source.my.ORANGE where a.id = b.id
{"result":[{
"query": "SELECT a.column1, b.column2
FROM abcd.hist a, efgh.present b
WHERE (select column3, column4 from UPS where a.id = b.id )"
},
{"query": "SELECT a.column1, b.column2
FROM apple.hist a, mango.present b
WHERE (select column3, column4 from my.ORANGE where a.id = b.id )"}
]}
您的结果是一个字典{},第一个关键字"result"包含字典{}的列表[],其中每个字典都有一个关键字"query",该关键字指向一个看起来像SQL查询的值。
假设你的输出对象被称为op,你可以得到你想要的结果如下:
for k in op['result']: # For each dictionary in result
print(str(k)[1:-1]) # Cast to a string and strip curlies
编辑:
def addSource(q):
lines = q.split("n")
for n,k in enumerate(lines):
if(k.startswith("FROM")):
q[n] = k.replace("FROM ","FROM source.").replace(", ",", source.")
return("n".join(q))
因此,
for n,k in enumerate(op['result']):
op['result'][n]["query"] = addSource(k["query"])