日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python删除两个excel表中的相同元素_python筛选出两个文件中重复行的方法

發布時間:2023/12/9 python 16 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python删除两个excel表中的相同元素_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删除两个excel表中的相同元素_python筛选出两个文件中重复行的方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。