需要用Python解析工具日志文件并将结果保存为Excel或CSV
我需要用Python来解析下面的日志文件:
Log file created: 9/10/2014 4:03:21 PM
----------------------------------------
Ticks = 2408967 <3360> <UpdatePlaybackStatusInfo> <0> Avg Prefetch(ms): 157.739, Avg Render(ms): 25.7375, Avg Display FPS: 27.3688
Ticks = 3371181 <3360> <UpdatePlaybackStatusInfo> <0> Frames dropped during playback: 0 / 219, Preroll(ms): 812.849
Ticks = 3371181 <3360> <UpdatePlaybackStatusInfo> <0> Avg Prefetch(ms): 17.1389, Avg Render(ms): 33.8339, Avg Display FPS: 29.5562
Ticks = 3465531 <10548> <Assert> <0> Debug Assert failed!
Ticks = 3465531 <10548> <Assert> <0> wglMakeCurrent failed: Error 0: The operation completed successfully.
我想提取<UpdatePlaybackStatusInfo>
和<Assert>
这两种记录,每种记录都要包含它的各个字段,然后把它们保存成Excel或CSV文件。
有没有人能帮我做到这一点呢?
1 个回答
1
我不太确定不等号里面的值是什么,所以我用foo
和bar
代替了它们。像这样应该可以解决问题:
import re
import csv
filtered_messages = ['UpdatePlaybackStatusInfo', 'Assert']
fieldnames = ['ticks', 'foo', 'type', 'bar', 'message']
with open('log.txt') as log:
with open('output.csv', 'w') as csv_file:
writer = csv.DictWriter(csv_file, delimiter=',', fieldnames=fieldnames)
writer.writerow(dict((fn,fn) for fn in fieldnames))
for line in log:
match = re.search(r'^Ticks = (?P<ticks>\d+)\s+<(?P<foo>\d+)> <(?P<type>\w+)> <(?P<bar>\d+)>\s+(?P<message>.+)$', line)
if match is not None and match.group('type') in filtered_messages:
writer.writerow(match.groupdict())
输出(以CSV格式):
ticks foo type bar message
2408967 3360 UpdatePlaybackStatusInfo 0 Avg Prefetch(ms): 157.739, Avg Render(ms): 25.7375, Avg Display FPS: 27.3688
3371181 3360 UpdatePlaybackStatusInfo 0 Frames dropped during playback: 0 / 219, Preroll(ms): 812.849
3371181 3360 UpdatePlaybackStatusInfo 0 Avg Prefetch(ms): 17.1389, Avg Render(ms): 33.8339, Avg Display FPS: 29.5562
3465531 10548 Assert 0 Debug Assert failed!
3465531 10548 Assert 0 wglMakeCurrent failed: Error 0: The operation completed successfully.