国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Python > 正文

python篩選出兩個文件中重復行的方法

2020-02-15 21:35:16
字體:
來源:轉載
供稿:網友

本文實例為大家分享了python腳本篩選出兩個文件中重復的行數,供大家參考,具體內容如下

'''查找A文件中,與B文件中內容不重復的內容'''#!usr/bin/pythonimport sysimport 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 0DPT = 100000 # DPT 是Data Per File的意思fileAName = sys.argv[1];fileBName = sys.argv[2];#STEP1:先拆掉B文件,作為比較基準,臨時文件命名為temp1,temp2,...,tempNprint("拆分比對文件.../n")fB = open(fileBName)tempFileNo = 1tempFileName = "temp{0}".format(tempFileNo)fTemp = open(tempFileName, "w+")line = fB.readline()lineCount = 0while 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 = 0fOut = open("repeat", "w+")repeatCount = 0for 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 - 1print("腳本結束。/n")

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林站長站。

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 山阳县| 临江市| 包头市| 拉孜县| 泗阳县| 泊头市| 宁晋县| 闵行区| 唐海县| 宁晋县| 铜鼓县| 西畴县| 河东区| 甘德县| 安新县| 保德县| 嵊泗县| 铁力市| 郴州市| 象州县| 嘉禾县| 太谷县| 汾阳市| 汨罗市| 珠海市| 胶州市| 汽车| 北票市| 渝中区| 东台市| 庆城县| 宁武县| 财经| 永胜县| 大关县| 龙泉市| 信阳市| 武平县| 师宗县| 金昌市| 宜君县|