Python processes text files and generates files in the specified format

Source: Internet
Author: User
This section describes how to process text files in python and generate files in the specified format. For more information, see
Import OS import sys import 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 rows of Error Data "findStr2 =" DBMS_OUTPUT.PUT_LINE ('processed" 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. 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 ()

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.