python method to filter out duplicate lines in two files

  • 2020-11-03 22:30:25
  • OfStack

This article shares the python script to screen out the number of duplicate lines in two files for your reference. The details are as follows


'''
 To find the A In the file, and B The content of a file that does not duplicate 
'''
#!usr/bin/python

import sys
import os

'''
 String lookup function, used 2 The sub - lookup method queries in the list 
'''
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  is Data Per File The meaning of 

fileAName = sys.argv[1];
fileBName = sys.argv[2];

#STEP1 : tear down first B File, as a comparison base, temporary file named as temp1,temp2,...,tempN
print(" Split alignment file ...\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(" When I'm done, 1 A total of {0} A temporary document, {1} The data. \n".format(tempFileNo, (tempFileNo-1)*DPT + lineCount))

#STEP2 : A File with the B The temporary files are compared one by one, and the results are written to the file in turn result0, result1
#     Last written result The file is the final result 
fA = open(fileAName)
resultTempFile = {"result0", "result1"};
tempIndex = 0
fOut = open("repeat", "w+")
repeatCount = 0
for i in range(1, tempFileNo + 1):
  print(" Compare the first {0} A temporary document ...\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(" The comparison is complete and the data is repeated {0} article ".format(repeatCount))

os.rename(resultTempFile, "result")

#STEP3 Delete all temporary files when you're done 
print(" Delete temporary files ...\n")
while tempFileNo > 0:
  tempFileName = "temp{0}".format(tempFileNo)
  os.remove(tempFileName)
  tempFileNo = tempFileNo - 1

print(" End of script. \n")

Related articles: