python处理文本文件并生成指定格式的文件
作者:whsnow 时间:2021-05-16 15:43:42
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()
标签:文本文件,指定格式
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Python 如何利用ffmpeg 处理视频素材
2022-05-31 19:25:17
![](https://img.aspxhome.com/file/2023/9/118639_0s.jpg)
Python实现基于SVM的分类器的方法
2023-11-18 18:20:02
PHP封装的PDO数据库操作类实例
2023-11-18 04:54:31
oracle 彻底删除方法
2009-07-02 12:22:00
Yii框架实现乐观锁与悲观锁流程详解
2023-11-16 13:38:38
关于SQL Server中索引使用及维护简介
2008-12-24 15:39:00
Python 获取windows桌面路径的5种方法小结
2023-04-01 09:06:47
Python读取Json字典写入Excel表格的方法
2021-06-20 06:33:58
![](https://img.aspxhome.com/file/2023/0/121920_0s.png)
Python编程实现简易的音乐播放器基本操作
2022-05-25 08:41:16
![](https://img.aspxhome.com/file/2023/5/80685_0s.jpg)
使用pycharm将自己项目代码上传github(小白教程)
2022-05-02 20:37:06
![](https://img.aspxhome.com/file/2023/6/82526_0s.jpg)
PyQt5 QLineEdit输入的子网字符串校验QRegExp实现
2022-05-08 16:47:58
![](https://img.aspxhome.com/file/2023/7/121437_0s.png)
django模板语法学习之include示例详解
2023-10-03 18:52:54
Python 实现把列表中的偶数变成他的平方
2023-12-13 02:45:18
![](https://img.aspxhome.com/file/2023/4/97544_0s.jpg)
一篇文章带你了解python集合基础
2022-04-09 14:10:17
![](https://img.aspxhome.com/file/2023/5/101145_0s.jpg)
5种Python单例模式的实现方式
2023-05-17 12:57:14
python创建学生成绩管理系统
2023-08-09 04:19:38
![](https://img.aspxhome.com/file/2023/9/62299_0s.jpg)
Python Numpy学习之索引及切片的使用方法
2021-09-04 02:59:01
![](https://img.aspxhome.com/file/2023/3/120943_0s.png)
python实现登录与注册系统
2022-04-26 02:32:38
![](https://img.aspxhome.com/file/2023/8/68328_0s.jpg)
详解Python中string模块除去Str还剩下什么
2021-08-25 12:48:19
Python的Scrapy爬虫框架简单学习笔记
2023-09-25 06:41:44