Python processes text files and generates files in the specified format

  • 2020-04-02 13:51:51
  • OfStack

import os 
import sys 
import string 
 
# Opens the specified file in the specified mode and gets the file handle  
def getFileIns(filePath,model): 
  print(" Open the file ") 
  print(filePath) 
  print(model) 
  return open(filePath,model) 
 
# Gets the files that need to be processed  
def getProcFile(path): 
  return os.listdir(path) 
 
# Determine if a condition is met, and if so, execute  
def isTrue(outFileIns,s): 
  findStr1 = "LINE_COUNT_UPDATE   INTEGER := 0;" 
  writeStr1 = "LINE_COUNT_ERROR    INTEGER := 0;    -- Wrong data XX article " 
  findStr2 = "DBMS_OUTPUT.PUT_LINE(' processed " 
  writeStr2 = "DBMS_OUTPUT.PUT_LINE(' Wrong data ['||LINE_COUNT_ERROR||'] article .');" 
  findStr3 = "DBMS_OUTPUT.PUT_LINE(' Insert data ['||CUR_RESULT.INT_ID||'] Abnormal occurrence at time ...');" 
  writeStr3 = "LINE_COUNT_ERROR := LINE_COUNT_ERROR+1;" 
  findStr4 = "DBMS_OUTPUT.PUT_LINE(' Update the data ['||CUR_RESULT.INT_ID||'] Abnormal occurrence at time ...');" 
   
  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("tttt"+writeStr3+"n") 
  elif s.find(findStr4) != -1: 
    outFileIns.write(s) 
    outFileIns.write("ttttt"+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("tttAND A.LONGITUDE IS NOT NULLntttAND A.LONGITUDE IS NOT NULLntttAND ROWNUM<2n") 
  elif s.find(") LOOP") != -1: 
    outFileIns.write("tt) LOOPn") 
  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()

Related articles: