This article mainly introduces how python processes text files to generate specified format files, which has some practical value, for more information about how to process text files in Python and generate files in the specified format, see the following code:
Import osimport sysimport string # Open the specified file in the specified mode and obtain the file handle def getFileIns (filePath, model): print ("open file") print (filePath) print (model) return open (filePath, model) # obtain the file to be processed def getProcFile (path): return OS. listdir (path) # determine whether a condition is met. If yes, run def isTrue (outFileIns, s): findStr1 = "LINE_COUNT_UPDATE INTEGER: = 0;" writeStr1 = "LINE_COUNT_ERROR INTEGER: = 0; -- XX "findStr2 =" DBMS_OUTPUT.PUT_LINE ('processed "writeSt R2 = "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. write (s) outFileIns. write ("\ t" + writeStr3 + "\ n") elif s. find (findStr4 )! =-1: outFileIns. write (s) outFileIns. write ("\ t" + writeStr3 + "\ n") elif s. find ("CS_OSLGIS ")! =-1: outFileIns. write (s. replace ("CS_OSLGIS", "CQ_RMW") elif s. find ("and a. longpolling> ")! =-1: outFileIns. write ("\ t \ tAND. longpolling is not null \ n \ t \ tAND. longpolling is not null \ n \ t \ tAND ROWNUM <2 \ n ") elif s. find (") LOOP ")! =-1: outFileIns. write ("\ t) LOOP \ n") else: outFileIns. write (s. replace ("| ')', 2", "| ')', 3") # read and process the text 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) outFileIns = getFileIns (outFilePath, outFileMod) getAndProc (inFileIns, outFileIns) inFileIns. close () outFileIns. close ()