python处理文本文件实现生成指定格式文件的方法
本文所述实例为Python处理文本文件并生成指定格式文件的方法,具体实现功能代码如下所示:
importos importsys importstring #以指定模式打开指定文件,获取文件句柄 defgetFileIns(filePath,model): print("打开文件") print(filePath) print(model) returnopen(filePath,model) #获取需要处理的文件 defgetProcFile(path): returnos.listdir(path) #判断是否满足某个条件,如果满足则执行 defisTrue(outFileIns,s): findStr1="LINE_COUNT_UPDATEINTEGER:=0;" writeStr1="LINE_COUNT_ERRORINTEGER:=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||']时发生异常...');" ifs.find(findStr1)!=-1: outFileIns.write(s) outFileIns.write(writeStr1+"\n") elifs.find(findStr2)!=-1: outFileIns.write(s) outFileIns.write(writeStr2+"\n") elifs.find(findStr3)!=-1: outFileIns.write(s) outFileIns.write("\t\t\t\t"+writeStr3+"\n") elifs.find(findStr4)!=-1: outFileIns.write(s) outFileIns.write("\t\t\t\t\t"+writeStr3+"\n") elifs.find("CS_OSLGIS")!=-1: outFileIns.write(s.replace("CS_OSLGIS","CQ_RMW")) elifs.find("ANDA.LONGITUDE>")!=-1: outFileIns.write("\t\t\tANDA.LONGITUDEISNOTNULL\n\t\t\tANDA.LONGITUDEISNOTNULL\n\t\t\tANDROWNUM<2\n") elifs.find(")LOOP")!=-1: outFileIns.write("\t\t)LOOP\n") else: outFileIns.write(s.replace("||')',2","||')',3")) #读取并处理文本 defgetAndProc(inFileIns,outFileIns): lines=inFileIns.readlines() forsinlines: #print(s) isTrue(outFileIns,s) if__name__=="__main__": inFileMod="r" outFileMod="w" path="D:\\rmsdata2gis" fortmpFileinos.listdir(path): inFilePath=path+"\\"+tmpFile outFilePath=path+"\\BAK_"+tmpFile inFileIns=getFileIns(inFilePath,inFileMod) outFileIns=getFileIns(outFilePath,outFileMod) getAndProc(inFileIns,outFileIns) inFileIns.close() outFileIns.close()