python处理文本文件实现生成指定格式文件的方法

(编辑:jimmy 日期: 2024/11/12 浏览:2)

本文所述实例为Python处理文本文件并生成指定格式文件的方法,具体实现功能代码如下所示:

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

一句话新闻

一文看懂荣耀MagicBook Pro 16
荣耀猎人回归!七大亮点看懂不只是轻薄本,更是游戏本的MagicBook Pro 16.
人们对于笔记本电脑有一个固有印象:要么轻薄但性能一般,要么性能强劲但笨重臃肿。然而,今年荣耀新推出的MagicBook Pro 16刷新了人们的认知——发布会上,荣耀宣布猎人游戏本正式回归,称其继承了荣耀 HUNTER 基因,并自信地为其打出“轻薄本,更是游戏本”的口号。
众所周知,寻求轻薄本的用户普遍更看重便携性、外观造型、静谧性和打字办公等用机体验,而寻求游戏本的用户则普遍更看重硬件配置、性能释放等硬核指标。把两个看似难以相干的产品融合到一起,我们不禁对它产生了强烈的好奇:作为代表荣耀猎人游戏本的跨界新物种,它究竟做了哪些平衡以兼顾不同人群的各类需求呢?