python篩選出兩個文件中重復(fù)行的方法
更新時間:2018年05月31日 11:10:39 作者:非完美主義者
這篇文章主要為大家詳細介紹了python篩選出兩個文件中重復(fù)行的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python腳本篩選出兩個文件中重復(fù)的行數(shù),供大家參考,具體內(nèi)容如下
'''
查找A文件中,與B文件中內(nèi)容不重復(fù)的內(nèi)容
'''
#!usr/bin/python
import sys
import os
'''
字符串查找函數(shù),使用二分查找法在列表中進行查詢
'''
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}條數(shù)據(jù)。\n".format(tempFileNo, (tempFileNo-1)*DPT + lineCount))
#STEP2:把A文件與B文件拆出來的臨時文件逐個進行比較,將結(jié)果輪流寫入文件result0, result1
# 最后寫入的result文件就是最終結(jié)果
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("比較完成,重復(fù)數(shù)據(jù){0}條".format(repeatCount))
os.rename(resultTempFile, "result")
#STEP3:結(jié)束后把臨時文件都刪掉
print("刪除臨時文件...\n")
while tempFileNo > 0:
tempFileName = "temp{0}".format(tempFileNo)
os.remove(tempFileName)
tempFileNo = tempFileNo - 1
print("腳本結(jié)束。\n")
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- python如何在列表、字典中篩選數(shù)據(jù)
- Python3.4實現(xiàn)從HTTP代理網(wǎng)站批量獲取代理并篩選的方法示例
- python獲取網(wǎng)頁中所有圖片并篩選指定分辨率的方法
- python素數(shù)篩選法淺析
- python使用篩選法計算小于給定數(shù)字的所有素數(shù)
- Python使用re模塊實現(xiàn)信息篩選的方法
- python 用正則表達式篩選文本信息的實例
- Python實現(xiàn)多條件篩選目標(biāo)數(shù)據(jù)功能【測試可用】
- Python cookbook(數(shù)據(jù)結(jié)構(gòu)與算法)篩選及提取序列中元素的方法
- Python實用技巧之列表、字典、集合中根據(jù)條件篩選數(shù)據(jù)詳解
相關(guān)文章
Keras預(yù)訓(xùn)練的ImageNet模型實現(xiàn)分類操作
這篇文章主要介紹了Keras預(yù)訓(xùn)練的ImageNet模型實現(xiàn)分類操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-07-07
Mysql數(shù)據(jù)庫反向生成Django里面的models指令方式
這篇文章主要介紹了Mysql數(shù)據(jù)庫反向生成Django里面的models指令方式,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-05-05
python 時間的訪問和轉(zhuǎn)換 time示例小結(jié)
Python 的 time 模塊提供了各種與時間處理相關(guān)的功能,包括獲取當(dāng)前時間、操作日期/時間以及執(zhí)行與時間相關(guān)的各種其它功能,這篇文章主要介紹了python 時間的訪問和轉(zhuǎn)換 time,需要的朋友可以參考下2024-05-05

