Python handles text file implementations to generate a specified format file _python

Source: Internet
Author: User

The example described in this article is a method for Python to process a text file and generate a specified format file, as shown in the following functional code:

Import OS import sys import string #以指定模式打开指定文件 get file handle def getfileins (Filepath,model): print (open file) print (FilePath) Print (model) return-open (Filepath,model) #获取需要处理的文件 def getprocfile (path): Return Os.listdir (Path) #判断是否满足某个条件, if
  Satisfied then execute def isTrue (outfileins,s): findStr1 = "Line_count_update INTEGER: = 0;"    WRITESTR1 = "Line_count_error INTEGER: = 0; --Error data xx strip "FINDSTR2 =" dbms_output. Put_Line (' finished "WRITESTR2 = Dbms_output. Put_Line (' Error data ' | | line_count_error| | ']
  article. '); FINDSTR3 = "Dbms_output." Put_Line (' Insert data [' | | cur_result.int_id| | ']
  When an exception occurs ... ');
  WRITESTR3 = "Line_count_error: = line_count_error+1;" FINDSTR4 = "Dbms_output." Put_Line (' Update data [' | | cur_result.int_id| | ']
  
  When an exception occurs ... ');
    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) outfi Leins.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.longitude are 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" F  or 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 Article

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.