欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

最新評論