介绍
这期内容当中小编将会给大家带来有关使用python怎么筛选出两个文件的重复行,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。
具体内容如下
& # 39; & # 39; & # 39; 查找一个文件中,与B文件中内容不重复的内容 & # 39;& # 39;& # 39; # ! usr/bin/python import 系统 import 操作系统 & # 39;& # 39;& # 39; 字符串查找函数,使用二分查找法在列表中进行查询 & # 39;& # 39;& # 39; def binarySearch(价值,,行): right 才能=,len(行),安康;1 left 才能=0 时间=a 才能;value.strip () while 才能left & lt;=,右: ,,,middle =, int((时间+ right left +, - 1)/2) ,,,b =,行(中间).strip () ,,,if a ==, b: ,,,,,return 1 ,,,if a & lt;, b: ,,,,,right =middle 作用;1 ,,,: ,,,,,left =, middle + 1 ,return 0 时间=DPT 100000, #, DPT 是Data Per 文件的意思 时间=fileAName sys.argv [1]; 时间=fileBName sys.argv [2]; #步骤1:先拆掉B文件,作为比较基准,临时文件命名为temp1, temp2,…, tempN print(“拆分比对文件…\ n") 时间=fB 开放(fileBName) tempFileNo =1 时间=tempFileName “临时{0}“.format (tempFileNo)=fTemp 开放(tempFileName,“w +“) 时间=line fB.readline () lineCount =0 while 线: if 才能;lineCount 祝辞=,DPT: ,,,fTemp.flush () ,,,fTemp.close () ,,,tempFileNo =, tempFileNo + 1 ,,,tempFileName =,“临时{0}“.format (tempFileNo) ,,,fTemp =,开放(tempFileName,,“w +“) ,,,lineCount =0 fTemp.write才能(线) 时间=lineCount 才能;lineCount + 1 时间=line 才能;fB.readline (),, fTemp.flush () fTemp.close () fB.close () print(“拆分完成,一共{0}个临时文件,{1}条数据\ n" .format (tempFileNo,, (tempFileNo-1) * DPT +, lineCount)) 步骤2:把一个文件与B文件拆出来的临时文件逐个进行比较,将结果轮流写入文件result0,, result1。编写此表达式 #,,,,最后写入的结果文件就是最终结果 时间=fA 开放(fileAName) resultTempFile =, {“result0",,“result1"}; tempIndex =0 fOut =,开放(“repeat",,“w +“) repeatCount =0 for 小姐:拷贝范围(1,tempFileNo +, 1): 打印才能(“比较第{0}个临时文件…\ n" .format(我)) if 才能;0,==,tempIndex: ,,,resultTempFile =,“result0",,, ,,,tempIndex =1 其他的才能: ,,,resultTempFile =,“result1" ,,,tempIndex =0 fResult =,开放才能(resultTempFile,,“w +“) fTemp =,开放才能(“临时{0}“.format(我)) 时间=lineSet 才能;fTemp.readlines () fTemp.close才能() lineList 才能=,列表(lineSet) lineList.sort才能() 时间=line 才能;fA.readline () while 才能;线: ,,,if 0,==, binarySearch(线,,lineList): ,,,,,fResult.write(线) ,,,: ,,,,,fOut.write(线) ,,,,,repeatCount =, repeatCount + 1 ,,,line =, fA.readline () fA.close才能() fResult.flush才能() fResult.close才能() fA =,开放才能(resultTempFile) fA.close () fOut.flush () fOut.close () print(“比较完成,重复数据{0}条“.format (repeatCount)) os.rename (resultTempFile,“result") 步骤3:结束后把临时文件都删掉 print(“删除临时文件…\ n") while tempFileNo 祝辞,0: tempFileName =,才能“临时{0}“.format (tempFileNo) os.remove才能(tempFileName) tempFileNo 才能=tempFileNo 作用;1 print(“脚本结束\ n")
Python主要用来做什么
Python主要应用于:1,网络开发;2、数据科学研究;3,网络爬虫;4、嵌入式应用开发,5日游戏开发;6桌面应用开发。
上述就是小编为大家分享的使用Python怎么筛选出两个文件的重复行了,如果刚好有类似的疑惑,不妨参照上述分析进行理解。如果想知道更多相关知识,欢迎关注行业资讯频道。