python筛选出两个文件中重复行的方法
作者:非完美主义者 时间:2021-02-16 12:53:04
本文实例为大家分享了python脚本筛选出两个文件中重复的行数,供大家参考,具体内容如下
'''
查找A文件中,与B文件中内容不重复的内容
'''
#!usr/bin/python
import sys
import os
'''
字符串查找函数,使用二分查找法在列表中进行查询
'''
def binarySearch(value, lines):
right = len(lines) - 1
left = 0
a = value.strip()
while left <= right:
middle = int((right + left + 1)/2)
b = lines[middle].strip()
if a == b:
return 1
if a < b:
right = middle - 1
else:
left = middle + 1
return 0
DPT = 100000 # DPT 是Data Per File的意思
fileAName = sys.argv[1];
fileBName = sys.argv[2];
#STEP1:先拆掉B文件,作为比较基准,临时文件命名为temp1,temp2,...,tempN
print("拆分比对文件...\n")
fB = open(fileBName)
tempFileNo = 1
tempFileName = "temp{0}".format(tempFileNo)
fTemp = open(tempFileName, "w+")
line = fB.readline()
lineCount = 0
while line:
if lineCount >= DPT:
fTemp.flush()
fTemp.close()
tempFileNo = tempFileNo + 1
tempFileName = "temp{0}".format(tempFileNo)
fTemp = open(tempFileName, "w+")
lineCount = 0
fTemp.write(line)
lineCount = lineCount + 1
line = fB.readline()
fTemp.flush()
fTemp.close()
fB.close()
print("拆分完成,一共{0}个临时文件,{1}条数据。\n".format(tempFileNo, (tempFileNo-1)*DPT + lineCount))
#STEP2:把A文件与B文件拆出来的临时文件逐个进行比较,将结果轮流写入文件result0, result1
# 最后写入的result文件就是最终结果
fA = open(fileAName)
resultTempFile = {"result0", "result1"};
tempIndex = 0
fOut = open("repeat", "w+")
repeatCount = 0
for i in range(1, tempFileNo + 1):
print("比较第{0}个临时文件...\n".format(i))
if 0 == tempIndex:
resultTempFile = "result0"
tempIndex = 1
else:
resultTempFile = "result1"
tempIndex = 0
fResult = open(resultTempFile, "w+")
fTemp = open("temp{0}".format(i))
lineSet = fTemp.readlines()
fTemp.close()
lineList = list(lineSet)
lineList.sort()
line = fA.readline()
while line:
if 0 == binarySearch(line, lineList):
fResult.write(line)
else:
fOut.write(line)
repeatCount = repeatCount + 1
line = fA.readline()
fA.close()
fResult.flush()
fResult.close()
fA = open(resultTempFile)
fA.close()
fOut.flush()
fOut.close()
print("比较完成,重复数据{0}条".format(repeatCount))
os.rename(resultTempFile, "result")
#STEP3:结束后把临时文件都删掉
print("删除临时文件...\n")
while tempFileNo > 0:
tempFileName = "temp{0}".format(tempFileNo)
os.remove(tempFileName)
tempFileNo = tempFileNo - 1
print("脚本结束。\n")
来源:https://blog.csdn.net/qyshooter/article/details/53508924
标签:python,筛选,重复行
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
基于python实现删除指定文件类型
2022-02-16 06:19:48
嵌入式Web视频点播系统实现方法
2007-10-10 21:17:00
学以致用 驳“ASP低能论”
2007-09-30 13:01:00
在Python文件中指定Python解释器的方法
2023-06-24 13:22:50
html风格tooltip效果的实现
2010-04-08 13:00:00
重新发现HTML表格
2009-12-02 09:47:00
![](https://img.aspxhome.com/file/UploadPic/200912/2/01-38s.png)
苹果的“创新”
2010-01-12 13:45:00
asp如何验证日期输入是否正确?
2010-06-10 18:38:00
详解用python计算阶乘的几种方法
2021-11-17 00:00:14
在Python中操作文件之seek()方法的使用教程
2023-08-01 14:58:01
一个简单的JS显示日期代码
2009-02-10 12:34:00
centos yum php 7.x 无需删除升级的方法
2023-11-20 14:48:08
![](https://img.aspxhome.com/file/2023/5/79385_0s.jpg)
详解pandas获取Dataframe元素值的几种方法
2022-12-28 07:30:01
Python中删除文件的几种方法实例
2021-02-02 05:57:13
简单方法实现网页自动适应任何分辨率任何窗口大小
2008-09-13 19:28:00
使用Python实现 学生学籍管理系统
2023-08-21 18:42:47
![](https://img.aspxhome.com/file/2023/6/90556_0s.png)
Pygame Rect区域位置的使用(图文)
2023-08-14 05:27:48
![](https://img.aspxhome.com/file/2023/5/77585_0s.gif)
python标准库之time模块的语法与简单使用
2023-06-11 17:42:23
![](https://img.aspxhome.com/file/2023/2/59142_0s.png)
Python实现的最近最少使用算法
2022-07-10 22:48:27
详解go语言json的使用技巧
2023-09-12 10:27:48