タイムスタンプ行ごとにデータを抽出しているのですが、重複したデータが1つと言う扱いになってしまいます。対処方法を知りたいです。
以下記述です。
なるべく以下の辞書型だとそもそも、同じタイムスタンプだった場合に処理不可なのでしょうか。
import os import csv import glob from collections import defaultdict def logparser(filepath): """ ログファイルからデータを抽出 """ datastore = defaultdict(dict) with open(filepath) as f: for textline in f.readlines(): try: textline = textline.rstrip('\n') items = textline.split() timestamp = textline[:23] datastore[timestamp]['タイムスタンプ'] = timestamp if items[-2] == 'INFO:': datastore[timestamp]['リクエスト内容'] = textline.split('INFO: ')[1] if 'type' in items: datastore[timestamp]['type'] = textline.split('type = ')[1] except: pass return datastore def export2csv(data, filename): """ csv形式で出力 """ header = ['タイムスタンプ', 'リクエスト内容', 'type'] filename = f'{filename}.csv' with open(filename, 'w') as f: csv.writer(f, lineterminator='\n').writerow(header) with open(filename, 'a') as f: for record in data.items(): record = [record[1].get(h) for h in header] csv.writer(f, lineterminator='\n').writerow(record) if __name__ == '__main__': """ 抽出からファイル出力までの一連の処理 """ for filepath in glob.glob('C:\Users\ik\py\log\log-*.txt'): filename, extention = os.path.splitext(filepath) data = logparser(filepath) export2csv(data, filename)
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。