查询sql脚本的所有来源表

import os
import re
# import pandas as pd
path_sql_file    = "/u/users/vn55gxn/py_file/python_source_file_sql"
# path_sql_file    = "/u/users/vn55gxn/py_from_file/from_soure"
path_target_file = "/u/users/vn55gxn/py_from_file/from_target"

# path_excle_file  = "/u/users/vn55gxn/py_from_file/python_source_file_excle/python_repace.xlsx"

def search_file(path_sql_file):
    pattern_from = r'from\s+(.*?)\s+'                                                                   #匹配from 后面两个空格之间的内容
    pattern_join = r'join\s+(.*?)\s+'
    pattern_tmp = re.compile(r'^(?!.*(?:tmp|temp)).*$')                                                 # 过滤了tmp|temp 这些表
    pattern_all  = r'from\s+(.*?)\s+|join\s+(.*?)\s+'
    match_from_list =[]
    with  open(path_sql_file, "r") as file_source:
        lines = file_source.readlines()
        file_source.close()

    # with open(path_target_file, "w") as file_target:  # 不存在会自动创建
    #     file_target.write('')  # 写入前先清空目标文件

        for line in lines:
            match_from = re.search(pattern_from,line)
            if match_from:
                if len(match_from.group(1))>10 and re.findall(pattern_tmp,match_from.group(1)):              #过滤一些不正确的匹配
                    # file_target.write(match_from.group(1)+"\n")   #写入匹配的结果
                    match_from_list.append(match_from.group(1))
                    # print(match_from.group(1))

            match_join = re.search(pattern_join,line)
            if match_join:
                if len(match_join.group(1))>10 and re.findall(pattern_tmp,match_join.group(1)):              #过滤一些不正确的匹配
                    # file_target.write(match_join.group(1)+"\n")  #写入匹配的结果
                    match_from_list.append(match_join.group(1))
                    # print(match_join.group(1))

    match_from_set=set(match_from_list)                                                                      #set去重
    for i in match_from_set:
        print(i)


        # file_target.close()


if __name__ == '__main__':
    print("开始------------------------------------")
    list_dir = os.listdir(path_sql_file)
    for i in list_dir:
        path_sql_file_dtl = path_sql_file + r"/" + i
        # path_target_file_dtl = path_target_file + r"/" + i
        #替换开始
        # search_file(path_sql_file_dtl,path_target_file_dtl)
        search_file(path_sql_file_dtl)
        print(path_sql_file_dtl + "          已完成")
        print("\n")

    # Python 字典(Dictionary) items() 函数以列表返回可遍历的(键, 值) 元组数组。

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值