我正在尝试搜索CSV文件的重复设备名称的行。输出应记录第一个匹配行的日期,并记录最后一行的日期。我需要一些关于从CSV文件中删除重复设备名称的逻辑的帮助,同时还要记录设备首次和最后一次出现的时间。搜索CSV的匹配字段,并使用初始日期
import time as epoch
# AlertTime, DeviceName, Status
Input = [['14/08/2016 13:00', 'device-A', 'UP'], ['14/08/2016 13:15', 'device-B', 'DOWN'], ['15/08/2016 17:30', 'device-A', 'UP']]
# FirstSeen, LastSeen, DeviceName, Status
Output = []
# Last 48 hours
now = epoch.time()
cutoff = now - (172800)
for i in Input:
AlertTime = epoch.mktime(epoch.strptime(i[0], '%d/%m/%Y %H:%M'))
if AlertTime > cutoff:
Result = [i[0], i[0], i[1], i[2]]
Output.append(Result)
print(Output)
输出(3项):
[['14/08/2016 13:00', '14/08/2016 13:00', 'device-A', 'UP'], ['14/08/2016 13:15', '14/08/2016 13:15', 'device-B', 'DOWN'], ['15/08/2016 17:30', '15/08/2016 17:30', 'device-A', 'UP']]
通缉输出(2项):
[['14/08/2016 13:15', '14/08/2016 13:15', 'device-B', 'DOWN'], ['14/08/2016 13:00', '15/08/2016 17:30', 'device-A', 'UP']]
用'device'关键和'(FirstSeen,LastSeen,设备名称使用字典,状态)'作为价值。 –
@VedangMehta也许你可以省略'DeviceName'字段,因为它已经是关键了?否则,我完全同意。 – bdvll
@bdvll你是完全正确的。 –