0
我有如下語句的SQL文件,聲明需要提取在分隔符';'之間提取sql語句使用python RE
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;
下面
是Python代碼中提取SQL語句和計數一次選擇關鍵字的數量出現在聲明中
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)從它獲取正則表達式中的語句?