这个python的代码,是用来查找日志里,记录的条码的重复数量。
#!/usr/bin/python
# -*- coding: UTF-8 -*-
#目前的字典会自动过滤【时间】完全一致的数据,需要注意
import datetime
import os
import sys
import math
reload(sys)
sys.setdefaultencoding('utf8')
def view_bar(Index):
Index="%02d" % Index #数字前面补齐0
sys.stdout.write(Index+'%->'+"\b\b\b\b\b")
sys.stdout.flush()
def Remove0D0A(tmpstr):
return tmpstr.strip().replace('\n', '').replace('\r', '')
def GetAllBarcode(filename):
searchstr='Applicator:'
l=len(searchstr)
pos ={}
with open(filename,'r') as file_to_read:
while True:
line = file_to_read.readline()
if not line :
break
pass
if line.find('\''+searchstr+'\'')>0:
continue
tindex=line.find('\t')
bindex=line.find(searchstr)
if bindex>0:
time=''
tmp=line[bindex+l:]
if tindex>0:
time=line[0:tindex]
else:
time=tmp #如果时间不存在,则以条码记录作为时间列
global Count
Count=Count+1
tmp=line[bindex+l:]
pos[time]=tmp
#print 'Time:',time,'Barcode:',tmp
#print pos
return pos
Count=0
start = datetime.datetime.now()
dir='DataFile'
files=os.listdir(dir)
print 'Analyze File Count:',len(files)
TimeBarcode={}
for a in files:
path=dir+'\\'+a
#print 'Path:',path
tmp=GetAllBarcode(path)
TimeBarcode.update(tmp)
#print tmp
Barcode=[]
f = open('record.csv','a+')
for time,value in TimeBarcode.items():
#print 'Time:',time,'Barcode:',value
time=Remove0D0A(time)
barcode=Remove0D0A(value)
Barcode.append(barcode)
f.write(time+',\''+barcode+'\r')
print 'Barcode Total Count:',len(Barcode)
read= datetime.datetime.now()
print 'Read use time:',str(read-start)
duplicated=set()
barcode_len=len(Barcode)
LastNum=0
for i in range(0,barcode_len):
Num=(float(i)/barcode_len)*100
if math.modf(Num)[1]-LastNum>0:
LastNum=Num
view_bar(LastNum)
if(Barcode[i] in Barcode[i+1:]):
duplicated.add(Barcode[i])
elif(i>1 and Barcode[i] in Barcode[0:i-1]):
duplicated.add(Barcode[i],)
if len(duplicated)>0:
f = open('duplicate.csv','a+')
for b in duplicated:
f.write('\''+b+'\r')
print b+'\r'
#print duplicated
else:
print 'No duplicate'
end= datetime.datetime.now()
print 'Analyze data use time:',str(end-read)
if Count>0:
print 'Time not exist:',Count