2016-04-27 128 views
0

我有一個大的日誌有幾個命令(結尾)和它們的輸出(至END)像下面這樣:Python的日誌分析器

<blabla; 

foo 
... 
... 

END 

<xyz; 

... 
... 

END 

--and so on 

的要求是與命令名稱不同的文件中像

blabla 
xyz 

並且在每個文件中應該是它們各自的輸出。

到目前爲止,我有:

def generateDicts(log_fh): 
currentDict = {} 
for line in log_fh: 
    if line.endswith(";"): 
     if line.endswith("END"): 
      yield currentDict 
     currentDict = {""} 
    else: 
     currentDict["text"] += line 
yield currentDict 

with open("logfile.txt") as f: 
print list(generateDicts(f)) 

請幫助。

+0

1)你的問題是什麼? 2)你的解決方案有什麼不足?它是否打印錯誤?它是否無法正確執行? –

回答

1

你的文章說你需要寫入文件,但你的例子沒有做任何文件I/O。這是一個打開,關閉和寫入文件的程序。

import fileinput 

output = None 
for line in fileinput.input(): 
    line2 = line.strip() 
    if line2.startswith('<'): 
     output = open(line2[1:].split(';')[0], 'w') 
    elif line2 == 'END': 
     output.close() 
     output = None 
    elif output: 
     output.write(line) 
+0

我在哪裏放置文件名? – slyclam

+0

你把文件名放在命令行上。將我的程序保存爲「parse_log.py」,然後運行以下命令:'python parse_log.py logfilename.log' –

+0

我運行了它。它使用第一個命令名稱生成一個空白文件。這是我得到的錯誤:>蟒蛇parse_log.py test.log中 回溯(最近通話最後一個): 文件 「parse_log.py」,9號線,在 output.close() AttributeError的: 'NoneType' 對象沒有屬性「關閉」 – slyclam

0

您可以使用re模塊

import re 
with open('test','r') as f,open('output','w') as f1: 
    f1.write("\n".join(re.findall(r'\<(\w+)\;',f.read()))) 

輸出:

blabla 
xyz 

但是,如果文件尺寸過大,則可以考慮從文件中的行讀取線,而不是讀它作爲一個整體。