python筛选出两个文件中重复行的方法

yipeiwu_com5年前Python基础

本文实例为大家分享了python脚本筛选出两个文件中重复的行数,供大家参考,具体内容如下

'''
查找A文件中,与B文件中内容不重复的内容
'''
#!usr/bin/python

import sys
import os

'''
字符串查找函数,使用二分查找法在列表中进行查询
'''
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}条数据。\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("比较完成,重复数据{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")

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Pytorch基本变量类型FloatTensor与Variable用法

Pytorch基本变量类型FloatTensor与Variable用法

pytorch中基本的变量类型当属FloatTensor(以下都用floattensor),而Variable(以下都用variable)是floattensor的封装,除了包含floa...

详解Python基础random模块随机数的生成

随机数参与的应用场景大家一定不会陌生,比如密码加盐时会在原密码上关联一串随机数,蒙特卡洛算法会通过随机数采样等等。Python内置的random模块提供了生成随机数的方法,使用这些方法时...

Python cookbook(数据结构与算法)从序列中移除重复项且保持元素间顺序不变的方法

本文实例讲述了Python从序列中移除重复项且保持元素间顺序不变的方法。分享给大家供大家参考,具体如下: 问题:从序列中移除重复的元素,但仍然保持剩下的元素顺序不变 解决方案: 1、如果...

Python imutils 填充图片周边为黑色的实现

Python imutils 填充图片周边为黑色的实现

代码 import imutils import cv2 image = cv2.imread('') # translate the image x=25 pixels to t...

python使用in操作符时元组和数组的区别分析

在python中可以使用in符号判断指定的元素是否存在于列表中,但我发现元组和数组存在区别,下面是详细实验结果。 >>> 'jb51.net' in ['haotu...