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

