Python 如何在SQL脚本中提取表名?

Python 如何在SQL脚本中提取表名?,python,parsing,Python,Parsing,假设存在sql脚本: select * from ( select col1 from test.test_a join test.test_a1 on a.col1 = a1.col1) a left join test.test_b b on a.col1 = b.col2 left join test.test_c c on b.col2 = c.col3 left jon (select col4 from test.test

假设存在sql脚本:

select *
from (
  select col1 from test.test_a join test.test_a1 on a.col1 = a1.col1) a
left join test.test_b b 
on a.col1 = b.col2
left join
    test.test_c c
on b.col2  = c.col3
left jon
   (select 
       col4 
    from
       test.test_d) d
on c.col3  = d.col4
我正在阅读并尝试使用python提取上述脚本中“from”或“join”之后的所有表名。 困难在于我正在逐行处理脚本,但是表名和关键字可能不在同一行中


那么如何从脚本中提取这样的表名呢?任何建议都行。

只要将所有多个空格序列(包括换行符)转换为一个空格,就可以得到一行,然后使用正则表达式查找表名

import re
sql = """select *
from (
  select col1 from test.test_a join test.test_a1 on a.col1 = a1.col1) a
left join test.test_b b 
on a.col1 = b.col2
left join
    test.test_c c
on b.col2  = c.col3
left join
   (select 
       col4 
    from
       test.test_d) d
on c.col3  = d.col4"""
sql_line = re.sub('\s+', ' ', sql)
tbl_re = re.compile(r'(?:\b(?:from)|(?:join)\b\s+)(\w+)\b')
tablenames = tbl_re.findall(sql_line)
print(tablenames)

请注意,表名提取regexp是简化的,仅作为一个示例,您必须考虑可能的引用等。

只需将所有多个空格序列(包括换行符)转换为一个空格,就可以得到一行,然后使用正则表达式cat查找表名

import re
sql = """select *
from (
  select col1 from test.test_a join test.test_a1 on a.col1 = a1.col1) a
left join test.test_b b 
on a.col1 = b.col2
left join
    test.test_c c
on b.col2  = c.col3
left join
   (select 
       col4 
    from
       test.test_d) d
on c.col3  = d.col4"""
sql_line = re.sub('\s+', ' ', sql)
tbl_re = re.compile(r'(?:\b(?:from)|(?:join)\b\s+)(\w+)\b')
tablenames = tbl_re.findall(sql_line)
print(tablenames)

请注意,表名提取regexp是简化的,仅作为一个示例,如果要使用核心python,您必须考虑可能的引用等

txt = """
select *
from (
  select col1 from test.test_a join test.test_a1 on a.col1 = a1.col1) a
left join test.test_b b 
on a.col1 = b.col2
left join
    test.test_c c
on b.col2  = c.col3
left jon
   (select 
       col4 
    from
       test.test_d) d
on c.col3  = d.col4"""

replace_list = ['\n', '(', ')', '*', '=']
for i in replace_list:
    txt = txt.replace(i, ' ')
txt = txt.split()
res = []
for i in range(1, len(txt)):
    if txt[i-1] in ['from', 'join'] and txt[i] != 'select': 
        res.append(txt[i])
print(res)

如果您想使用核心python:

txt = """
select *
from (
  select col1 from test.test_a join test.test_a1 on a.col1 = a1.col1) a
left join test.test_b b 
on a.col1 = b.col2
left join
    test.test_c c
on b.col2  = c.col3
left jon
   (select 
       col4 
    from
       test.test_d) d
on c.col3  = d.col4"""

replace_list = ['\n', '(', ')', '*', '=']
for i in replace_list:
    txt = txt.replace(i, ' ')
txt = txt.split()
res = []
for i in range(1, len(txt)):
    if txt[i-1] in ['from', 'join'] and txt[i] != 'select': 
        res.append(txt[i])
print(res)

下面是@r.user.05apr答案之上的快速改进。合并来自


下面是@r.user.05apr答案之上的快速改进。合并来自


它与python的关系如何?它与python的关系如何?这太糟糕了。。。哈哈。我知道我们不应该发表赞美,但是哇,这很简单。现在我将要处理一些大型SQL文件,所以我们将看到它在性能方面的表现,但是哇!这么简单…这太恶心了。。。哈哈。我知道我们不应该发表赞美,但是哇,这很简单。现在我将要处理一些大型SQL文件,所以我们将看到它在性能方面的表现,但是哇!这么简单。。。