需求:
数据源:
UserA,LocaltionA,2018-01-01 08:00:00,60 UserA,LocaltionA,2018-01-01 09:00:00,60 UserA,LocaltionB,2018-01-01 10:00:00,60 UserA,LocaltionA,2018-01-01 11:00:00,60 UserA,LocaltionA,2018-01-01 12:00:00,56 UserA,LocaltionA,2018-01-01 13:00:00,60 UserA,LocaltionB,2018-01-01 23:00:00,60 UserA,LocaltionB,2018-01-02 00:00:00,60 UserA,LocaltionB,2018-01-02 01:00:00,60 UserB,LocaltionB,2018-01-02 08:50:00,10 UserB,LocaltionB,2018-01-02 09:00:00,60 UserB,LocaltionB,2018-01-02 10:00:00,23
代码块:
#!/usr/bin/env python3 # -*- coding:utf-8 -*- import datetime def time_dif(time1,time2): time1=datetime.datetime.strptime(time1,"%Y-%m-%d %H:%M:%S") time2=datetime.datetime.strptime(time2,"%Y-%m-%d %H:%M:%S") if time1 < time2: return (time2-time1).seconds else: return (time1-time2).seconds with open("input.csv",'r') as f: data=f.readlines() f.close() i=0 print("========处理前=========") for msg in data: print(msg.strip().split(',')) while True: if i < len(data): print("i=%d" % i) j=i+1 while j <= len(data): print("\tj=%d"%j) User, Localtion, Atime, Stander = data[i].strip().split(',') user,localtion,atime,stander=data[i+1].strip().split(',') if User == user and Localtion == localtion and time_dif(Atime, atime) == int(Stander) * 60: data.pop(i+1) data.pop(i) res = int(Stander) + int(stander) new_data = '%s,%s,%s,%s\n' % (User, Localtion, Atime, res) data.insert(i, new_data) else: break j+=1 i+=1 if (i+2) > len(data): print("========处理后=========") for after in data: print(after.strip().split(',')) break
结果:
========处理后========= ['UserA', 'LocaltionA', '2018-01-01 08:00:00', '120'] ['UserA', 'LocaltionB', '2018-01-01 10:00:00', '60'] ['UserA', 'LocaltionA', '2018-01-01 11:00:00', '116'] ['UserA', 'LocaltionA', '2018-01-01 13:00:00', '60'] ['UserA', 'LocaltionB', '2018-01-01 23:00:00', '180'] ['UserB', 'LocaltionB', '2018-01-02 08:50:00', '93']
未完待续。。。。