Python根据文件记录替换脚本中字符
import os import sys import re import cx_Oracle class FileReplace(): def __init__(self,username1,passwd1,username2,passwd2,jdbc_url): self.username1 = username1 self.passwd1 = passwd1 self.username2 = username2 self.passwd2 = passwd2 self.jdbc_url = jdbc_url def oracle_connect_database(self,username,passwd,jdbc_url): conn = cx_Oracle.connect(username,passwd,jdbc_url) cursor = conn.cursor() return conn,cursor def select_table(self,history_table,username,passwd,jdbc_url): username = username passwd = passwd jdbc_url = jdbc_url conn,cursor = self.oracle_connect_database(username,passwd,jdbc_url) table_column_name = "select column_name from user_tab_columns where table_name = '{}'".format(history_table) select_table_cmd = "select \"script\" from {}".format(history_table) table_script_list = [] try: cursor.execute("{}".format(table_column_name)) column_result = cursor.fetchall() cursor.execute(select_table_cmd) history_result = cursor.fetchall() for script in history_result: table_script_list.append(list(script)) except cx_Oracle.DatabaseError as e: x = e.args[0] print("{} is not exist,retrun {}".format(history_table,x.code)) return False finally: cursor.close() conn.close() print("{} script list:{}".format(history_table,table_script_list)) return table_script_list #将从数据库中读取出来的数据进行list拆分 def flatten(self,script_list): if isinstance(script_list, list): for i in script_list: for element in self.flatten(i): yield element else: yield script_list #对脚本进行比较 def compareAndCheck(self,table1,table2,): v_list,r_list,other_list= self.getFile() list1 = self.select_table(table1,self.username1,self.passwd1,self.jdbc_url) list2 = self.select_table(table2,self.username2,self.passwd2,self.jdbc_url) r1 = list(self.flatten(list1)) table_list = [] for script in r1: if '/' in script: table_list.append(script.split('/')[-1]) else: table_list.append(script) r2 = list(self.flatten(list2)) for script in r2: if '/' in script: table_list.append(script.split('/')[-1]) else: table_list.append(script) for v in v_list: if v.split('\\')[-1] in [t for t in table_list]: print("[not replace :] {} ".format(v)) else: self.loopReplace() #读取文件内容方法 def getFile(self): cwd_path = os.path.abspath(os.path.dirname(sys.argv[0])) os.chdir(cwd_path) #定义各类型脚本的存放列表 v_list = [] r_list = [] other_list = [] for root,dirs,files in os.walk(cwd_path,topdown=True): for file in files: if file.startswith("V") and file.endswith("sql"): if '__' not in file: print("[error],file {} is less __".format(file)) else: v_result = file v_list.append(os.path.join(root,v_result)) #print("V startswith file:",os.path.join(root,file)) elif file.startswith("R") and file.endswith("sql"): r_result = file r_list.append(os.path.join(root,r_result)) #print("R startswith file:",os.path.join(root,file)) else: other_result = file other_list.append(os.path.join(root,other_result)) #print("other startwith file",os.path.join(root,file)) return v_list,r_list,other_list def getReplaceStr(self): file = 'replace.txt' str_dict = {} with open(file,'r',encoding='utf-8') as f: content = f.readlines() for i in content: str_dict.update({i.split("=")[0].strip():i.split("=")[-1].strip()}) return str_dict def loopReplace(self): v_list,r_list,other_list= self.getFile() cwd_path = os.path.abspath(os.path.dirname(sys.argv[0])) os.chdir(cwd_path) replace_file = 'replace.txt' if os.path.exists(replace_file): replace_dict = self.getReplaceStr() for v in v_list: for replace_before,replace_after in replace_dict.items(): self.readAndReplaceContent(v,replace_before,replace_after) else: print("exit is not exists replace.txt ,not replace ") def readAndReplaceContent(self,file,replace_before,replatr_after): with open(file,'r',encoding='utf-8') as f1: content = f1.readlines() with open(file,'w',encoding='utf-8') as f2: for replace_str in content: if replace_before in replace_str: print("[replace file :]{}".format(file)) print("[replace before]:{} = [replace after]:{}".format(replace_before,replace_before)) replace_str = replace_str.replace(replace_before,replatr_after) f2.write(replace_str) else: f2.write(replace_str) #读取文件内容方法 def readContent(self,file): with open(file,'r',encoding='utf-8') as f: content = f.read() return content if __name__ == '__main__': db_name1 = sys.argv[1] db_passwd1 = sys.argv[2] db_name2 = sys.argv[3] db_passwd2 = sys.argv[4] jdbc_url = sys.argv[5] table_name1 = sys.argv[6] table_name2 = sys.argv[7] fp = FileReplace(db_name1,db_passwd1,db_name2,db_passwd2,jdbc_url) fp.oracle_connect_database(db_name1,db_passwd1,jdbc_url) fp.oracle_connect_database(db_name2,db_passwd2,jdbc_url) fp.compareAndCheck(table_name1,table_name2)
#Python#
Python 文章被收录于专栏
Python由荷兰数学和计算机科学研究学会的吉多·范罗苏姆于1990年代初设计,作为一门叫做ABC语言的替代品。Python提供了高效的高级数据结构,还能简单有效地面向对象编程。Python语法和动态类型,以及解释型语言的本质,使它成为多数平台上写脚本和快速开发应用的编程语言,随着版本的不断更新和语言新功能的添加,逐渐被用于独立的、大型项目的开发