python处理文本文件并生成指定格式的文件

WBOY
发布: 2016-06-06 11:32:13
原创
1065 人浏览过

import os 
import sys 
import string 
 
#以指定模式打开指定文件,获取文件句柄 
def getFileIns(filePath,model): 
  print("打开文件") 
  print(filePath) 
  print(model) 
  return open(filePath,model) 
 
#获取需要处理的文件 
def getProcFile(path): 
  return os.listdir(path) 
 
#判断是否满足某个条件,如果满足则执行 
def isTrue(outFileIns,s): 
  findStr1 = "LINE_COUNT_UPDATE   INTEGER := 0;" 
  writeStr1 = "LINE_COUNT_ERROR    INTEGER := 0;    --错误数据XX条" 
  findStr2 = "DBMS_OUTPUT.PUT_LINE('处理完毕" 
  writeStr2 = "DBMS_OUTPUT.PUT_LINE('错误数据['||LINE_COUNT_ERROR||']条.');" 
  findStr3 = "DBMS_OUTPUT.PUT_LINE('插入数据['||CUR_RESULT.INT_ID||']时发生异常...');" 
  writeStr3 = "LINE_COUNT_ERROR := LINE_COUNT_ERROR+1;" 
  findStr4 = "DBMS_OUTPUT.PUT_LINE('更新数据['||CUR_RESULT.INT_ID||']时发生异常...');" 
   
  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\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 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) 
    outFileIns = getFileIns(outFilePath,outFileMod) 
    getAndProc(inFileIns,outFileIns) 
    inFileIns.close() 
    outFileIns.close()
登录后复制

相关标签:
来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板