使用python RE在分隔符“;”之间提取sql语句

2024-09-28 03:18:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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)?你知道吗


Tags: namefromreidcount语句resultquery
1条回答
网友
1楼 · 发布于 2024-09-28 03:18:02

在你拥有的地方

q = {result :{ 'count_select': count_select}}

你可以替代

q = {re.sub(r'[\n\r]', '', result) :{ 'count_select': count_select}}

相关问题 更多 >

    热门问题