Import Osimport sysimport string# opens the specified file in a specific mode, gets the file handle def getfileins (Filepath,model): Print ("Open file") print (FilePath) Print (model) return open (Filepath,model) #获取须要处理的文件def getprocfile (path): Return Os.listdir (PATH) #推断是否满足某个条件, assuming that the Then run Def isTrue (outfileins,s): findStr1 = "Line_count_update INTEGER: = 0;" WRITESTR1 = "Line_count_error INTEGER: = 0; --Error data xx bar "FINDSTR2 =" dbms_output. Put_Line (' processing complete ' WRITESTR2 = ' dbms_output. Put_Line (' ERROR data [' | | | line_count_error| | '] '); " FINDSTR3 = "Dbms_output. Put_Line (' Insert data [' | | cur_result.int_id| | '] An exception occurred ... '); " WRITESTR3 = "Line_count_error: = line_count_error+1;" FINDSTR4 = "Dbms_output. Put_Line (' Update data [' | | cur_result.int_id| | '] An exception occurred ... '); " If S.find (findStr1)! = -1:outfileins.write (s) outfileins.write (writestr1+ "\ n") elif S.find (FINDSTR2)! = -1:outfileins.write (s) outfileins.write (writestr2+ "\ n") elif S.find (FINDSTR3)! = -1:OUTFILEINS.W Rite (s) outfilEins.write ("\t\t\t\t" +writestr3+ "\ n") elif S.find (FINDSTR4)! = -1:outfileins.write (s) outfileins.write (" \t\t\t\t\t "+writestr3+" \ n ") elif S.find (" Cs_oslgis ")! = -1:outfileins.write (S.replace (" Cs_oslgis "," CQ_RMW ")) Elif S.find ("and A.longitude >")! = -1:outfileins.write ("\t\t\tand a.longitude is not Null\n\t\t\tand a.longit UDE is not Null\n\t\t\tand rownum<2\n ") elif s.find (") LOOP ")! = -1:outfileins.write (" \t\t) loop\n ") Else : Outfileins.write (s.replace ("| |) ', 2", "| |) ', 3")) #读取并处理文本def Getandproc (infileins,outfileins): lines = Infileins.readlines () for S in lines: #print (s) isTrue (outfileins,s) if __name__== "__main__": Infilemod = "r" outfilemod = "W" path = "D:\\rmsdata2gis" for Tmpfile in Os.listdir (path): Infilepath = path+ "\ \" +tmpfile Outfilepath = path+ "\\BAK_" +tmpfile infileins = Getfileins (infilepath,infilemod) o Utfileins = Getfileins (oUtfilepath,outfilemod) Getandproc (infileins,outfileins) infileins.close () Outfileins.close ()
Copyright notice: This article blog original article. Blogs, without consent, may not be reproduced.
Python text file to generate the specified file format