Python中如何移除时间列表中相邻近的时间记录并保存至CSV?
移除时间间隔极近的记录并保存至CSV的问题
我是Python新手,现有如下格式的时间列表:
'2021-01-23 06:14:16' '2021-01-23 06:14:17' '2021-01-23 06:19:35' '2021-01-23 07:05:03' '2021-01-23 07:05:04' '2021-01-23 07:05:33' '2021-01-23 07:05:34'
我需要移除其中时间间隔极近的记录(如示例中相邻1秒的记录),最终得到如下结果并保存至CSV文件:
'2021-01-23 06:14:16' '2021-01-23 06:19:35' '2021-01-23 07:05:03' '2021-01-23 07:05:33'
我尝试了以下代码,但无法正确运行,恳请帮忙解决:
data_detections = pd.read_csv('path/to/file.csv') date_detec = data_detections.Fecha_UTC.values.tolist() time_detec = data_detections.Hora_UTC.values.tolist() com_date = data_detections.Fecha_UTC.values.tolist() com_time = data_detections.Hora_UTC.values.tolist() file_csv = open('path/to/file2.csv', "w") file_csv.write('Fecha_UTC,Hora_UTC\n') for t1,d1 in zip(time_detec,date_detec): h1 = "".join(t1[0:2]) m1 = "".join(t1[3:5]) s1 = "".join(t1[6:]) tm_comp = d1+','+h1+':'+m1 for t2,d2 in zip(com_time,com_date): h2 = "".join(t2[0:2]) m2 = "".join(t2[3:5]) s2 = "".join(t2[6:]) dd = "".join(d2[8:]) mm = "".join(d2[5:7]) yy = "".join(d2[0:4]) time_UTC = d2+','+h2+':'+m2 if tm_comp == time_UTC: if s1 == s2: pass elif int(s2)-10 < int(s1) < int(s2)+10: if s1 == s2: print('Same') elif int(s1)-10 < int(s2) < int(s1)+10: if s1 != s2: print(tm_comp+':'+str(s1)) file_csv.write(tm_comp+':'+str(s1)+'\n') else: continue
内容的提问来源于stack exchange,提问作者user25589007
相关产品推荐
相关产品推荐

