python篩選出兩個文件中重復行的方法
更新時間:2018年05月31日 11:10:39 作者:非完美主義者
這篇文章主要為大家詳細介紹了python篩選出兩個文件中重復行的方法,具有一定的參考價值,感興趣的小伙伴們可以參考一下
本文實例為大家分享了python腳本篩選出兩個文件中重復的行數(shù),供大家參考,具體內(nèi)容如下
''' 查找A文件中,與B文件中內(nèi)容不重復的內(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文件拆出來的臨時文件逐個進行比較,將結果輪流寫入文件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("比較完成,重復數(shù)據(jù){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")
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
您可能感興趣的文章:
- 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)多條件篩選目標數(shù)據(jù)功能【測試可用】
- Python cookbook(數(shù)據(jù)結構與算法)篩選及提取序列中元素的方法
- Python實用技巧之列表、字典、集合中根據(jù)條件篩選數(shù)據(jù)詳解
相關文章
python批量修改圖片尺寸,并保存指定路徑的實現(xiàn)方法
今天小編就為大家分享一篇python批量修改圖片尺寸,并保存指定路徑的實現(xiàn)方法,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2019-07-07詳解PyTorch預定義數(shù)據(jù)集類datasets.ImageFolder使用方法
這篇文章主要為大家介紹了PyTorch預定義數(shù)據(jù)集類datasets.ImageFolder使用方法詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04Python 中的 XML 轉(zhuǎn)換利器xml2dict詳解
xml2dict是一個Python庫,可以將XML數(shù)據(jù)轉(zhuǎn)換為字典,也支持反向轉(zhuǎn)換,它簡化了XML的處理,使之像處理JSON一樣簡單,適用于Web服務數(shù)據(jù)交換、配置文件讀取等場景,安裝簡單,使用方便,還可以通過自定義轉(zhuǎn)換器處理XML屬性和命名空間2024-10-10Python3.x+pycharm+Anaconda中縮小打包的.exe體積的問題
這篇文章主要介紹了Python3.x+pycharm+Anaconda中縮小打包的.exe體積的問題,本文通過圖文實例相結合給大家分享解決方案,需要的朋友可以參考下2021-08-08使用Python實現(xiàn)將PDF轉(zhuǎn)為圖片
這篇文章主要為大家詳細介紹了python如何借用第三方庫Spire.PDF for Python,從而實現(xiàn)將PDF轉(zhuǎn)為圖片的功能,感興趣的小伙伴可以跟隨小編一起學習一下2023-10-10