2017-05-26 93 views
-4

沒有運行? 1月23日16:54:16 pfsense php:rc.start_packages: 命令'/usr/local/etc/rc.d/radiusd.sh stop'返回退出代碼'1', 輸出'radiusd not running? 「我想解析系統日誌文件爲csv格式

Jan 23 16:54:19 pfsense php:rc.start_packages:[FreeRADIUS]:XMLRPC 同步以超時150秒開始。

年01月23 16點54分19秒pfsense PHP:rc.start_packages:[FreeRADIUS的]:XMLRPC Sync與'20 .20.20.2' 中止由於以下錯誤(S): 錯誤配置的複製目標端口。

Jan 23 16:54:19 pfsense php:rc.start_packages:[FreeRADIUS]:XMLRPC 同步正在結束。

年01月23 16點54分19秒pfsense PHP:rc.start_packages:該命令 '/usr/local/etc/rc.d/radiusd.sh停止' 返回退出代碼 '1',則輸出 被'半徑不運行?'

Jan 23 16:54:21 pfsense php:rc.start_packages:[FreeRADIUS]:XMLRPC 同步以超時150秒開始。

我想通過python解析上面的系統日誌文件的數據到一個csv文件。首先我想下面的代碼

import csv 
    import itertools 

    with open('system.log', 'r') as in_file: 
     stripped = (line.strip() for line in in_file) 
     lines = (line for line in stripped if line) 
     grouped = zip(*[lines] * 7) 
     with open('system.csv', 'w') as out_file: 
      writer = csv.writer(out_file) 
      writer.writerow(('month', 'day', 
    'time','pfsense','type','package','comment')) 
      writer.writerows(grouped) 

頭是完全到來,但該文件並沒有真正轉化爲csv。因此,我將文件轉換爲分隔文本文件並使用以下代碼進行分析。

import csv 
    with open('systemExcel.txt', "r") as infile, open('systeExcel.csv', 'w') 
    as outfile:in_txt = csv.reader(infile, delimiter = '\t') 
     out_csv = csv.writer(outfile) 
     out_csv.writerows(in_txt) 

上面的代碼工作完全正常。但我需要一個腳本,可以將原始日誌文件數據轉換爲帶有標題的csv文件。

+1

您可以將您的代碼你做了什麼? –

+0

'導入CSV 進口itertools 張開( 'SYSTEM.LOG', 'R')作爲in_file中: 剝離=(line.strip(),用於in_file中線) 線=(用於逐行在剝離如果線) 作爲out_file,打開('system.csv','w')分組= (分組) Writer.writerows(分組) @JayParikh –

+0

你能編輯你的問題並以適當的格式發佈你的代碼嗎?這是不可讀的評論 –

回答

1

問題:...我需要一個腳本,可以將原始日誌文件數據轉換成CSV文件與頭。

只讀取和寫入一個一行數據。
考慮這方面的例子:

with open('system.csv', 'w') as out_file, 
    open('system.log', 'r') as in_file: 

    writer = csv.writer(out_file) 
    writer.writerow(['month', 'day','time', 'pfsense', 'type', 'package', 'comment']) 

    for line in in_file: 
     columns = line[:-1].split(' ') 
     columns[6] = ' '.join(columns[6:]) 
     writer.writerow(columns[:7]) 

輸出
月,日,時,pfsense,類型,封裝,評論 月,23,16:54:16,pfsense,PHP :, rc.start_packages:,命令...(爲簡明起見,省略) Jan,23,16:54:19,pfsense,php:,rc.start_packages:,[FreeRADIUS]:XMLRPC ...(爲簡潔起見省略) Jan,23,16:54:19,pfsense,php:,rc.start_packages:,[FreeRADIUS]:XMLRPC ...(簡稱略) Jan,23,16:54:19,pfsense,php:,rc .start_packages:,[FreeRADIUS]:XMLR PC ...(爲了簡潔起見省略) Jan,23,16:54:19,pfsense,php:,rc.start_packages:,命令...(爲簡潔起見省略) Jan,23,16:54:21 ,pfsense,php:,rc.start_packages:,[FreeRADIUS]:XMLRPC ...(不再贅述)

測試使用Python 3.4.2

+0

謝謝! @stovfl –