[英]Using Python: how to get table names from sql query and add a word before the table schema where schemas are present
I have a json file that I am using as a dictionary in Python. 我有一个JSON文件,正在Python中用作字典。 The json file is very large.
json文件很大。 I am trying to write a python code to update each "query" by adding a "source."
我正在尝试编写python代码以通过添加“源”来更新每个“查询”。 before the table schema.
在表架构之前。 then use the updated dictionary for other programming purposes.
然后将更新后的字典用于其他编程目的。
The SQL scripts could have joins, cartesian joins, subqueries, etc. SQL脚本可以具有联接,笛卡尔联接,子查询等。
Expected output: 预期产量:
"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 )"}
]}
Your result is a dictionary {}, with the first key 'result' containing a list [] of dictionaries {} where each dictionary has a key 'query' that goes to a value that looks like a SQL query. 您的结果是一个字典{},其中第一个键“结果”包含字典{}的列表[],其中每个字典都有一个键“ query”,其键值类似于SQL查询。
Assuming your output object is called op you can get your desired result as follows: 假设您的输出对象称为op,则可以得到所需的结果,如下所示:
for k in op['result']: # For each dictionary in result
print(str(k)[1:-1]) # Cast to a string and strip curlies
EDIT: 编辑:
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))
and with that, 然后
for n,k in enumerate(op['result']):
op['result'][n]["query"] = addSource(k["query"])
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.