python文字檔案,生成指定的檔案格式

2021-09-07 01:39:02 字數 2306 閱讀 4609

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處理文字檔案實現生成指定格式檔案的方法

下面是具體 希望對大家有幫助 可以在下面複製 import os import sys import string 以指定模式開啟指定檔案,獲取檔案控制代碼 def getfileins filepath,model print 開啟檔案 print filepath print model ret...

python 生成文字檔案 43

import os def get fname while true fname input filename if not os.path.exists fname os.path.exists 檢視檔案是否存在 break print s already exists.try again fna...

python 讀寫文字檔案

本人最近新學python 用到文字檔案的讀取,經過一番研究,從網上查詢資料,經過測試,總結了一下讀取文字檔案的方法.a f open filename r content f.read decode utf 8 b f codecs.open encoding utf 8 content f.rea...