2014-09-10 51 views
-4

我需要解析以下日誌文​​件在Python的結果或CSV:需要解析Python中的工具,日誌文件,然後保存在Excel

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文件。

任何人都可以幫助我做到這一點。

+2

解析如何?你想要提取什麼? – 2014-09-10 12:36:13

+1

你的代碼在哪裏?它的問題究竟是什麼? – jonrsharpe 2014-09-10 12:41:49

+0

我想提取截至目前的值。請幫忙。 – 2014-09-10 13:00:45

回答

1

我不確定不等號所包含的值是什麼,所以我用foobar替換了它們。像這樣的東西應該做的伎倆:

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.