我有含次嵌套列表和一些對應的信息,並正嘗試通過第二提取從從彼此遵循次塊的開始一行避免類型錯誤(例如,10 :04:23,10:04:24,10:04:25。)。應該有很多這些小塊。我不確定我所擁有的是否正確,如果是這樣,它會引發一個TypeError,我不知道如何繞過它。使用時timedelta比較倍
這是與動物的訪問的區域數據,並記錄採取的每一秒。我的目標是每次訪問只有一個錄音,因此是一段時間後的第一行。
previous_and_next從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被盜是提高TypeError: Can't convert 'datetime.timedelta' object to str implicitly
,我明白了,但不知道如何避免。我甚至不確定這段代碼如何做我想做的事。
謝謝你的幫助!
d [1]是一個字符串...我想你想'aftersecond =項[-1] +的日期時間。 timedelta(秒= 1)'...其實d [-1]也將是一個字符串... – 2012-07-30 15:28:01