I have a nested list containing times and some corresponding information, and am trying to extract one line from the start of a block of times that follow on from each other by a second (e.g. 10:04:23,10:04:24,10:04:25..). There should be a lot of these little blocks. I'm not sure if what I have is on the right lines, and if it is, it raises a TypeError and I'm not sure how to get around it.
This is data relating to visits of animals to an area, and recordings are taken every second. My aim is to have only one recording per visit, hence the first line from a block of following-on times.
previous_and_next is stolen from here
data=[['07/11/2012', '09:53:36', 'U', '#0F', '0006E7895B', 'T', 'U\n', '09:53:36'],
['05/13/2012', '09:54:27', 'U', '#0F', '0006E3DADA', 'T', 'U\n', '5031', '09:54:27'] etc]
#define a function to get previous and following values
from itertools import tee, islice, chain
def previous_and_next(some_iterable):
prevs, items, nexts = tee(some_iterable, 3)
prevs = chain([None], prevs)
nexts = chain(islice(nexts, 1, None), [None])
return zip(prevs, items, nexts)
#convert times to datetime objects
for d in data:
try:
f=datetime.datetime.strptime(d[1],'%H:%M:%S')
g=f.strftime('%H:%M:%S')
d.append(g)
except:
pass
new_list=[]
for prev,item,next in previous_and_next(data):
aftersecond=item[1]+datetime.timedelta(seconds=1)
if next[1]==aftersecond: #if next time is this time plus a second
this=True
else:
this==False
while this==True:
continue
else:
new_list.append(data)
aftersecond is raising TypeError: Can't convert 'datetime.timedelta' object to str implicitly
, which I understand, but don't understand how to avoid. I'm not even certain this code does what I want it to do.
Thank you for your help!