使用python3爬去sql文件中所有新建以及修改的表名称

使用python3爬去sql文件中所有新建以及修改的表名称

import os
import pandas as pd
import chardet

def walk_all_files(rootdir,query_word):
    file_lst=[]
    create_lst=[]
    tabel_lst=[]
    for parent,dirnames,fielnames in os.walk(rootdir):
        # for dirname in dirnames:  
        #     print(dirname)        # 打印该目录下的所有文件夹
        #     pass
        for fielname in fielnames:
            if '.sql' in fielname:
                fiel_dir = os.path.join(parent,fielname)   # 文件所属地址
                print('----'+fiel_dir)
                coding = chardet.detect(open(fiel_dir,'rb').read())['encoding']     # 获取对应文件类型的字符集
                if coding == 'GB2312':      # 字符集GB2312无法读取,更换为GB18030
                    coding='GB18030'
                else:
                    pass
                file_lst2,create_lst2,tabel_lst2 = is_file_contain_word(fiel_dir,query_word,coding)
                file_lst+=file_lst2
                create_lst+=create_lst2
                tabel_lst+=tabel_lst2
    return file_lst,create_lst,tabel_lst
def is_file_contain_word(filedir,query_word,coding):
    file_lst1=[]
    create_lst1=[]
    tabel_lst1=[]
    filecontext=open(filedir,'r',encoding=coding).read()            # 读取文件
    if query_word in filecontext.lower().replace(' ',''):           # 文件内容小写、去空格后,判断关键字是否存在
        lines=filecontext.split('\n')                               # 文件按行转为列表
        for line in lines:
            if query_word in line.lower().replace(' ',''):          # 所属行内容小写、去空格后,判断关键字是否存在
                file_dir = filedir[2::]                             # 获取文件地址
                query_word_line=line.lstrip()                       # 获取行内容
                tabel_name=query_word_line.split(' ')[2]            # 获取表名称
                if '(' in tabel_name:                               # 表名称后面是否存在(
                    ind=tabel_name.index('(')
                    tabel_name=tabel_name[:ind]
                else:
                    pass
                file_lst1.append(file_dir)
                create_lst1.append(query_word_line)
                tabel_lst1.append(tabel_name)
    return file_lst1,create_lst1,create_lst1

rootdir = 'D:\\00_银河_工作记录'      # 地址参数
query_create='createtable'          # 关键字参数  

file_lst,create_lst,tabel_lst = walk_all_files(rootdir,query_create)     # 调用函数
df_create = pd.DataFrame(file_lst)                                       # 转为df格式
df_create['query_word_line']=create_lst                                  # df格式新增列
df_create['table_name']=tabel_lst

df_create.to_csv('get_table_name_create.csv',index=False)                # df格式写入csv

rootdir = 'D:\\00_银河_工作记录'
query_alter='createtable'

file_lst,create_lst,tabel_lst = walk_all_files(rootdir,query_alter)
df_create = pd.DataFrame(file_lst)
df_create['query_word_line']=create_lst
df_create['table_name']=tabel_lst

df_create.to_csv('get_table_name_alter.csv',index=False)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值