我有一个SQL文件与以下语句,该语句需要被提取
select * from customers;
select count(*) from customers;
select a.cust_name,sum(b.revenue) from
customers a join revenue_tab b
on a.c_id=b.c_id
group by a.cust_name;
下面是提取sql语句并计算SELECT关键字出现在语句中的时间的python代码
import re
query = {}
def GetTheStatements():
with open('dummy.sql') as fp:
for result in re.findall('(.*?);', fp.read(), re.S):
count_select = sum(1 for x in re.finditer(r"\bselect\b", result))
q = {result :{ 'count_select': count_select}}
query.update(q)
print query
GetTheStatements()
但最终的字典将如下所示
{'\n\nselect count(*) from customers': {'count_select': 1}, '\nselect * from customers': {'count_select': 1}, ' \n\nselect a.cust_name,sum(b.revenue) from\ncustomers a join revenue_tab b \non a.c_id=b.c_id\ngroup by a.cust_name': {'count_select': 1}}
在获取正则表达式中的语句时,如何有时从新行(\n)中删除(\r)?你知道吗
在你拥有的地方
你可以替代
相关问题 更多 >
编程相关推荐