我目前正在嘗試對按1秒時間間隔發送的消息進行分組。我目前正在計算時間延遲與此:按時間間隔對消息進行分組
def time_deltas(infile):
entries = (line.split() for line in open(INFILE, "r"))
ts = {}
for e in entries:
if " ".join(e[2:5]) == "T out: [O]":
ts[e[8]] = e[0]
elif " ".join(e[2:5]) == "T in: [A]":
in_ts, ref_id = e[0], e[7]
out_ts = ts.pop(ref_id, None)
yield (float(out_ts),ref_id[1:-1],(float(in_ts)*1000 - float(out_ts)*1000))
INFILE = 'C:/Users/klee/Documents/test.txt'
import csv
with open('test.csv', 'w') as f:
csv.writer(f).writerows(time_deltas(INFILE))
不過,我想計算的「T在:[A]」的數量派出每秒的消息,並已試圖與這個合作,這樣做的:
import datetime
import bisect
import collections
data=[ (datetime.datetime(2010, 2, 26, 12, 8, 17), 5594813L),
(datetime.datetime(2010, 2, 26, 12, 7, 31), 5594810L),
(datetime.datetime(2010, 2, 26, 12, 6, 4) , 5594807L),
]
interval=datetime.timedelta(seconds=50)
start=datetime.datetime(2010, 2, 26, 12, 6, 4)
grid=[start+n*interval for n in range(10)]
bins=collections.defaultdict(list)
for date,num in data:
idx=bisect.bisect(grid,date)
bins[idx].append(num)
for idx,nums in bins.iteritems():
print('{0} --- {1}'.format(grid[idx],len(nums)))
可以在這裏找到:Python: group results by time intervals
(我知道單位會關閉我想要的東西,但我只是尋找到的總體思路...)
到目前爲止,我一直未能成功,並希望得到任何幫助。
而且,出現 的數據爲:
082438.577652 - T in: [A] accepted. ordID [F25Q6] timestamp [082438.575880] RefNumber [6018786] State [L]
謝謝!這很令人驚訝。 :D – eunhealee 2012-01-13 18:09:26
非常歡迎。 – 2012-01-13 19:20:24
對不起,我該如何將它寫入csv文件? – eunhealee 2012-01-13 19:49:56