在云端留个备份,纯粹自己编的,初学状态,编的很繁琐。
import datetime,time
from dateutil import parser
import pandas as pd
import numpy as np
###############Step1:筛选指定的检测器
#li_mac按顺序存放指定的检测器
print('Step1')
#####################################!!!!!!!!!!!!!!!!!!!!!!此处修改指定的检测器名
li_mac=['80_Rocklin','80_Roseville_Pkwy','80_Lead_Hill','80_Darling_Way','80_Westwood_Park','80_Foothill_Farms','80_Madison_EB','80_Split','80_Pinell']
#打开原始数据
la = pd.read_csv("Iteris_bt_02-19-2018.txt",names=['a','b','time','detection','mac'],header=None)
la=la.drop(['a','b'],axis=1)
print(la.head(5))
#将指定检测器的数据放在shaixuanman文件中。
############################!!!!!!!!!!!!!!!!!!!!此处依据li_mac修改
lb = la.loc[(la["detection"]==li_mac[0]) |(la["detection"]==li_mac[1]) | (la["detection"]==li_mac[2]) | (la["detection"]==li_mac[3])| (la["detection"]==li_mac[4])| (la["detection"]==li_mac[5])| (la["detection"]==li_mac[6])| (la["detection"]==li_mac[7])| (la["detection"]==li_mac[8])]
lb.to_excel("shaixuanmac.xlsx",header=True,index=False)
counts = lb['mac'].value_counts()
counts.to_excel("jishu.xlsx")
#清空内存
del la
del lb
###############END
######Step2
print('Step2')
doc = open('new-result.txt','w')
lc = pd.read_excel("shaixuanmac.xlsx",names=['hour','detection','mac'],header=None)
#print(lc.head(5))
l_count = pd.read_excel("jishu.xlsx",names=['mac','counts'],header=None)
l_counts=l_count.drop([0]) #删去jishu的第一行
del l_count #清空内存
#print(l_counts.head(5))
l_counts = l_counts[l_counts['counts'] > 1] #此处修改
for i in l_counts.index:
l_counts_mac=l_counts.loc[i,'mac']
for j in lc.index:
lc_mac=lc.loc[j,'mac']
if lc_mac==l_counts_mac:
mac_result=str(lc.loc[j,'hour'])+' '+lc.loc[j,'detection']+' '+lc.loc[j,'mac']
print(mac_result,file=doc)
doc.close()
del l_counts_mac
del mac_result
del lc_mac
del lc
##########################END
###########Step3
print('Step3')
##################!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!此处修改指定的检测器名
File1 = open('NEW-NETHOD-last-result01.txt','w')
File2 = open('NEW-NETHOD-last-result12.txt','w')
File3 = open('NEW-NETHOD-last-result23.txt','w')
File4 = open('NEW-NETHOD-last-result34.txt','w')
File5 = open('NEW-NETHOD-last-result45.txt','w')
File6 = open('NEW-NETHOD-last-result56.txt','w')
File7 = open('NEW-NETHOD-last-result67.txt','w')
File8 = open('NEW-NETHOD-last-result78.txt','w')
mac_classify = pd.read_table('new-result.txt',names=['hour','ap','detection','mac'],header = None,delim_whitespace=True)
print('mac_classify=',mac_classify.head())
for ione in l_counts.index: #l_counts.index
i_mac=str(l_counts.loc[ione,'mac'])
column_mac=mac_classify[mac_classify.mac.isin([i_mac])]
#print(column_mac.head())
column_mac=column_mac.reset_index()
#print(column_mac.head())
column_mac_detection=column_mac['detection']
A=list(column_mac_detection)
#################!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!以下依据检测器设置修改
if li_mac[0] in A and li_mac[1] in A : #^!^此处可修改
ll = len(column_mac.index)
for itwo in range(0,ll-1):
if column_mac.loc[itwo,'detection'] == li_mac[0] and column_mac.loc[itwo+1,'detection'] == li_mac[1]:
t11=parser.parse(str(column_mac.loc[itwo,'hour']))
t12=parser.parse(str(column_mac.loc[itwo+1,'hour']))
dt1 = (t12-t11).seconds #80_Richards与80_Chiles之间<1500秒
if dt1 < 2400 :
result1 = str(column_mac.loc[itwo,'hour']) +' '+str(column_mac.loc[itwo,'ap'])+' '+str(column_mac.loc[itwo,'detection']) +' '+str(column_mac.loc[itwo,'mac']) +"\n" + str(column_mac.loc[itwo+1,'hour']) +' '+str(column_mac.loc[itwo+1,'ap'])+' '+str(column_mac.loc[itwo+1,'detection']) +' '+str(column_mac.loc[itwo+1,'mac'])
File1.write(result1+'\n')
File1.flush()#######
if li_mac[1] in A and li_mac[2] in A : #^!^此处可修改
ll = len(column_mac.index)
for itwo in range(0,ll-1):
if column_mac.loc[itwo,'detection'] == li_mac[1] and column_mac.loc[itwo+1,'detection'] == li_mac[2]:
t21=parser.parse(str(column_mac.loc[itwo,'hour']))
t22=parser.parse(str(column_mac.loc[itwo+1,'hour']))
dt2 = (t22-t21).seconds #80_Richards与80_Chiles之间<1500秒
if dt2 < 2400 :
result2 = str(column_mac.loc[itwo,'hour']) +' '+str(column_mac.loc[itwo,'ap'])+' '+str(column_mac.loc[itwo,'detection']) +' '+str(column_mac.loc[itwo,'mac']) +"\n" + str(column_mac.loc[itwo+1,'hour']) +' '+str(column_mac.loc[itwo+1,'ap'])+' '+str(column_mac.loc[itwo+1,'detection']) +' '+str(column_mac.loc[itwo+1,'mac'])
File2.write(result2+'\n')
File2.flush()#######
if li_mac[2] in A and li_mac[3] in A : #^!^此处可修改
ll = len(column_mac.index)
for itwo in range(0,ll-1):
if column_mac.loc[itwo,'detection'] == li_mac[2] and column_mac.loc[itwo+1,'detection'] == li_mac[3]:
t31=parser.parse(str(column_mac.loc[itwo,'hour']))
t32=parser.parse(str(column_mac.loc[itwo+1,'hour']))
dt3 = (t32-t31).seconds #80_Richards与80_Chiles之间<1500秒
if dt3 < 2400 :
result3 = str(column_mac.loc[itwo,'hour']) +' '+str(column_mac.loc[itwo,'ap'])+' '+str(column_mac.loc[itwo,'detection']) +' '+str(column_mac.loc[itwo,'mac']) +"\n" + str(column_mac.loc[itwo+1,'hour']) +' '+str(column_mac.loc[itwo+1,'ap'])+' '+str(column_mac.loc[itwo+1,'detection']) +' '+str(column_mac.loc[itwo+1,'mac'])
File3.write(result3+'\n')
File3.flush()#######
if li_mac[3] in A and li_mac[4] in A : #^!^此处可修改
ll = len(column_mac.index)
for itwo in range(0,ll-1):
if column_mac.loc[itwo,'detection'] == li_mac[3] and column_mac.loc[itwo+1,'detection'] == li_mac[4] :
t41=parser.parse(str(column_mac.loc[itwo,'hour']))
t42=parser.parse(str(column_mac.loc[itwo+1,'hour']))
dt4 = (t42-t41).seconds #80_Richards与80_Chiles之间<1500秒
if dt4 < 2400 :
result4 = str(column_mac.loc[itwo,'hour']) +' '+str(column_mac.loc[itwo,'ap'])+' '+str(column_mac.loc[itwo,'detection']) +' '+str(column_mac.loc[itwo,'mac']) +"\n" + str(column_mac.loc[itwo+1,'hour']) +' '+str(column_mac.loc[itwo+1,'ap'])+' '+str(column_mac.loc[itwo+1,'detection']) +' '+str(column_mac.loc[itwo+1,'mac'])
File4.write(result4+'\n')
File4.flush()#######
if li_mac[4] in A and li_mac[5] in A : #^!^此处可修改
ll = len(column_mac.index)
for itwo in range(0,ll-1):
if column_mac.loc[itwo,'detection'] == li_mac[4] and column_mac.loc[itwo+1,'detection'] == li_mac[5] :
t51=parser.parse(str(column_mac.loc[itwo,'hour']))
t52=parser.parse(str(column_mac.loc[itwo+1,'hour']))
dt5 = (t52-t51).seconds #80_Richards与80_Chiles之间<1500秒
if dt5 < 2400 :
result5 = str(column_mac.loc[itwo,'hour']) +' '+str(column_mac.loc[itwo,'ap'])+' '+str(column_mac.loc[itwo,'detection']) +' '+str(column_mac.loc[itwo,'mac']) +"\n" + str(column_mac.loc[itwo+1,'hour']) +' '+str(column_mac.loc[itwo+1,'ap'])+' '+str(column_mac.loc[itwo+1,'detection']) +' '+str(column_mac.loc[itwo+1,'mac'])
File5.write(result5+'\n')
File5.flush()#######
if li_mac[5] in A and li_mac[6] in A : #^!^此处可修改
ll = len(column_mac.index)
for itwo in range(0,ll-1):
if column_mac.loc[itwo,'detection'] == li_mac[5] and column_mac.loc[itwo+1,'detection'] == li_mac[6] :
t61=parser.parse(str(column_mac.loc[itwo,'hour']))
t62=parser.parse(str(column_mac.loc[itwo+1,'hour']))
dt6 = (t62-t61).seconds #80_Richards与80_Chiles之间<1500秒
if dt6 < 2400 :
result6 = str(column_mac.loc[itwo,'hour']) +' '+str(column_mac.loc[itwo,'ap'])+' '+str(column_mac.loc[itwo,'detection']) +' '+str(column_mac.loc[itwo,'mac']) +"\n" + str(column_mac.loc[itwo+1,'hour']) +' '+str(column_mac.loc[itwo+1,'ap'])+' '+str(column_mac.loc[itwo+1,'detection']) +' '+str(column_mac.loc[itwo+1,'mac'])
File6.write(result6+'\n')
File6.flush()#######
if li_mac[6] in A and li_mac[7] in A : #^!^此处可修改
ll = len(column_mac.index)
for itwo in range(0,ll-1):
if column_mac.loc[itwo,'detection'] == li_mac[6] and column_mac.loc[itwo+1,'detection'] == li_mac[7] :
t71=parser.parse(str(column_mac.loc[itwo,'hour']))
t72=parser.parse(str(column_mac.loc[itwo+1,'hour']))
dt7 = (t72-t71).seconds #80_Richards与80_Chiles之间<1500秒
if dt7 < 2400 :
result7 = str(column_mac.loc[itwo,'hour']) +' '+str(column_mac.loc[itwo,'ap'])+' '+str(column_mac.loc[itwo,'detection']) +' '+str(column_mac.loc[itwo,'mac']) +"\n" + str(column_mac.loc[itwo+1,'hour']) +' '+str(column_mac.loc[itwo+1,'ap'])+' '+str(column_mac.loc[itwo+1,'detection']) +' '+str(column_mac.loc[itwo+1,'mac'])
File7.write(result7+'\n')
File7.flush()#######
if li_mac[7] in A and li_mac[8] in A : #^!^此处可修改
ll = len(column_mac.index)
for itwo in range(0,ll-1):
if column_mac.loc[itwo,'detection'] == li_mac[7] and column_mac.loc[itwo+1,'detection'] == li_mac[8] :
t81=parser.parse(str(column_mac.loc[itwo,'hour']))
t82=parser.parse(str(column_mac.loc[itwo+1,'hour']))
dt8 = (t82-t81).seconds #80_Richards与80_Chiles之间<1500秒
if dt8 < 2400 :
result8 = str(column_mac.loc[itwo,'hour']) +' '+str(column_mac.loc[itwo,'ap'])+' '+str(column_mac.loc[itwo,'detection']) +' '+str(column_mac.loc[itwo,'mac']) +"\n" + str(column_mac.loc[itwo+1,'hour']) +' '+str(column_mac.loc[itwo+1,'ap'])+' '+str(column_mac.loc[itwo+1,'detection']) +' '+str(column_mac.loc[itwo+1,'mac'])
File8.write(result8+'\n')
File8.flush()#######
###########################
#####################################END
del mac_classify
del i_mac
del column_mac_detection
del A
del column_mac
del result1
del result2
del result3
del result4
del result5
del result6
del result7
del result8
##########################################################################################3
###############################第四步
print('Step4')
B=['01','12','23','34','45','56','67','78']
for iB in range(len(B)):
oldplot_mac = pd.read_table('NEW-NETHOD-last-result'+B[iB]+'.txt',names=['hour','ap','detection','mac'],header = None,delim_whitespace=True)
#print(oldplot_mac.head())
####将12小时制转换为24小时制
for itime in oldplot_mac.index:
if oldplot_mac.loc[itime,'ap']== 'AM':
if oldplot_mac.loc[itime,'hour'] > "11:59:59":
old_time=datetime.datetime.strptime(oldplot_mac.loc[itime,'hour'],"%H:%M:%S")
#new_time=(old_time +datetime.timedelta(hours=36)).strftime("%H:%M:%S")
oldplot_mac.loc[itime,'hour'] = (old_time +datetime.timedelta(hours=36)).strftime("%H:%M:%S")
elif oldplot_mac.loc[itime,'ap']== 'PM':
if oldplot_mac.loc[itime,'hour'] < "12:00:00":
old_time=datetime.datetime.strptime(oldplot_mac.loc[itime,'hour'],"%H:%M:%S")
#new_time=(old_time +datetime.timedelta(hours=12)).strftime("%H:%M:%S")
oldplot_mac.loc[itime,'hour'] = (old_time +datetime.timedelta(hours=36)).strftime("%H:%M:%S")
######
#对时间做升序排列
oldplot_mac=oldplot_mac.sort_values(["hour"])
#####
#print(oldplot_mac.head())
#将同一个mac都归到一起
docone = open('Step4mac_plot'+B[iB]+'.txt','w')
#######待删除
#########
for ione in l_counts.index:
l_counts_mac=l_counts.loc[ione,'mac']
for j in oldplot_mac.index:
lc_mac=oldplot_mac.loc[j,'mac']
if lc_mac==l_counts_mac:
mac_result=str(oldplot_mac.loc[j,'hour'])+' '+oldplot_mac.loc[j,'ap']+' '+oldplot_mac.loc[j,'detection']+' '+oldplot_mac.loc[j,'mac']
print(mac_result,file=docone)
#######
docone.close()
###################
del oldplot_mac
del l_counts_mac
del mac_result
doctwo = open('LAST_RESULT-'+B[iB]+'.txt','w')
plot_mac = pd.read_table('Step4mac_plot'+B[iB]+'.txt',names=['hour','ap','detection','mac'],header = None,delim_whitespace=True)
print('第二步')
len_excel=len(plot_mac.index)
for i in range(0,len_excel,2):
result = str(plot_mac.loc[i,'mac'])+' ' +str(plot_mac.loc[i,'detection']) +' '+str(plot_mac.loc[i+1,'detection'])+' '+str(plot_mac.loc[i,'hour'])+' '+str(plot_mac.loc[i+1,'hour'])
print(result,file=doctwo)
doctwo.close()
print('结束')