我是Python的新手,我正在尝试编写一个脚本来提取日志的几行行的TimeStamps。
日志看起来像这样。
-----------------------------------------------
138976 2013-12-17 18:25:58.607 superman(759) Debug: {event} TUNE: IR Signal Received: Key Code: 104
138977 2013-12-17 18:25:58.616 mediamanager(763) Debug: {event} TUNE: Player Close Issued
138999 2013-12-17 18:25:58.683 mediamanager(763) Debug: {event} TUNE: Player Open Issued: atsc://fr783000000.256.10
139003 2013-12-17 18:25:58.688 mediamanager(763) Debug: {event} TUNE: Streamer Opened: atsc://fr783000000.256.10 SessionId=152 ProcessID=856
139014 2013-12-17 18:25:58.695 halserver(628) Debug: {event} TUNE: Frontend Locked: Tuner: 1
139030 2013-12-17 18:25:58.735 mediamanager(763) Debug: {event} TUNE: Player Play Issued: SessionId: 152
139133 2013-12-17 18:25:59.705 httpmediaserver(824) Debug: {event} TUNE: HTTP Request Received: HEAD /mediasession?producer_id=8&type=tune_live HTTP/1.1
139134 2013-12-17 18:25:59.757 halserver(628) Debug: {event} TUNE: New Picture Received
139136 2013-12-17 18:25:59.757 halserver(628) Debug: {event} TUNE: Video Is Visible
---------------------------------------
基本上,其间几乎没有其他不需要的线路。 这里很少有线条随机出现,我正在寻找的是当Tune事件出现在这个序列中时提取数据。
任何人都可以对此有所了解。
提前致谢:-)
答案 0 :(得分:4)
我仍然不确定你在问什么。您要么只想收集列表中的两个事件之间的时间戳,要么想要根据日志文件找出两个事件之间的时间?
import re
with open('my_log.txt') as my_log:
a = [re.findall(r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}.\d{3}',
line)[0] for line in my_log.readlines() if 'TUNE:' in line]
print a
如果您想要列表中的时间戳,请输出:
['2013-12-17 18:25:58.607', '2013-12-17 18:25:58.616',
'2013-12-17 18:25:58.683', '2013-12-17 18:25:58.688',
'2013-12-17 18:25:58.695', '2013-12-17 18:25:58.735',
'2013-12-17 18:25:59.705', '2013-12-17 18:25:59.757',
'2013-12-17 18:25:59.757']
如果您想知道第一个和最后一个事件之间的时间差异,请输出:
from dateutil import parser
t_delta = parser.parse(a[-1]) - parser.parse(a[0])
print t_delta #0:00:01.150000