需要在python中解析工具日志文件,然后将结果保存到excel或cs中

2024-09-26 18:05:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我需要用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>和{}记录,每个记录都有其组成字段,并将它们保存为Excel或CSV文件。在

有人能帮我吗。在


Tags: 文件logdisplay记录assertrenderfilems
1条回答
网友
1楼 · 发布于 2024-09-26 18:05:09

{{cdm}不确定用什么符号来代替。像这样的事情应该能做到:

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):

^{pr2}$

相关问题 更多 >

    热门问题