前置内容背景:
有两份100w的随机生成数据,每生成100w数据我都利用excel的去重功能将其中重复数据去除
但是我需要保持两百万数据也依旧不出现重复,而wps和office等excel格式工具最大支持编辑1048576行,不能将二者添加在一起再去使用excel的去重功能
尝试思路:
1.简单粗暴
逐个比较,从A数据中取一个与B数据中全部数据进行比较,这种比较方式效率较低,可以先进行排序后再去用二分比较提高对比速度
python对比代码:
#!/usr/bin/python
#CSVtoXML.py
#encoding:utf-8
import csv, os
from xml.dom.minidom import Document
def check_data(CAID1,CAID2):
if(CAID1 is CAID2):
return True
else:
return False
def checkXMLFile(filePrefix):
csvFile = open(filePrefix+'.csv');
headLine = csvFile.readline()
typeList = headLine.split(',')
doc = Document()
dataRoot = doc.createElement('ProdLogs')
dataRoot.setAttribute('xmlns:xsi', "http://www.w3.org/2001/XMLSchema-instance")
dataRoot.setAttribute('xsi:noNamespaceSchemaLocation', filePrefix+'.xsd')
doc.appendChild(dataRoot)
dataHeader = doc.createElement('Header')
dataHeader.setAttribute('STBManufacturerName','cmp')
dataHeader.setAttribute('FormatVersion','01.00.00')
dataRoot.appendChild(dataHeader)
dataLog = doc.createElement('Logs')
dataLog.setAttribute('CreationDate','2021/03/30 10:51')
total = 0
csvReader = csv.reader(csvFile)
tempReader = csv.reader(csvFile)
#print(len(csvReader))
i = 0
j = 0
data_list = [];
data_list1 = [];
for line in csvReader:
data_list.append(line[0])
data_list1.append(line[1])
#print(type(line))
for i in range(len(data_list)):
temp = data_list[i]
#print("cmp str is:")
#print(temp)
dataElt = doc.createElement('LogRecord')
for j in range(len(data_list1)):
#print(temp,data_list1[j])
if(check_data(temp,data_list1[j])):
print("NG")
dataElt.setAttribute("error",temp)
total+=1
dataLog.appendChild(dataElt)
dataLog.setAttribute('NumberOfRecords',str(total))
dataRoot.appendChild(dataLog)
xmlFile = open(filePrefix+'.xml','w')
xmlFile.write(doc.toprettyxml(indent = '\t'))
xmlFile.close()
def main():
for root, dirs, files in os.walk(os.getcwd()):
for fname in files:
index = fname.find('.csv')
if index > 0:
#print index, fname[:index]
checkXMLFile(fname[:index])
print("Transform " + fname + " OK!")
if __name__ == '__main__':
main()
input("Game Over!")
2.数据库导入,很多数据库都支持excel格式导入功能,将字段设置属性为唯一,即可保证数据不重复,但是后续导入B数据的时候插入的重复内容如何拦截错误这些处理细节感觉实现较为麻烦
3.借助轮子工具,有一些对比工具例如:beyond compare等,可以将两份数据筛选出来重复的部分,然后手动删除B数据中的重复内容就好,但是前提也需要对数据进行排序后效率才会比较高,乱序比较需要等待较久