How to extract all the table names from a Python script

176 views Asked by At

I have python script which has many SQL queries. I want to

spark.sql("Select a,b from schema.table1 UNION ALL Select a,b from schema.table2 ")

I need to extract all the table names referred in the script.

I need help on how to approach this?Can I pass the script as input file and search for matching pattern or is there any other better approach?

1

There are 1 answers

0
Xiaomin Wu On
  1. Split the str into a list of words like ["Select", "a", "b",...]
  2. define a set which contains all the sql keywords, eg: Select, All, schema and so on.
  3. filter the list in step 1 which not in the set in step 1.