码迷,mamicode.com
首页 > 编程语言 > 详细

python文本文件,生成指定的文件格式

时间:2015-08-08 19:41:57      阅读:170      评论:0      收藏:0      [点我收藏+]

标签:

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()
    
    
    

版权声明:本文博客原创文章。博客,未经同意,不得转载。

python文本文件,生成指定的文件格式

标签:

原文地址:http://www.cnblogs.com/mengfanrong/p/4713663.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!