Created
December 16, 2017 20:03
-
-
Save Alykoff/dead0af9ec8f9120062e4037e20542a4 to your computer and use it in GitHub Desktop.
log_parser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#log1.log | |
#20171215102030 00 qgyd 12345 | |
#20171215102032 00 qgyd 2 | |
#20171215102035 02 qgyd 3 | |
#20171215102039 00 qgyd 4 | |
# python3 | |
import os | |
import time | |
import glob | |
def transpose(data): | |
data_trans = [] | |
for i in range(0, 4): | |
data_trans.append([None] * len(data)) | |
for i in range(0, len(data)): | |
for j in range(0, 4): | |
data_trans[j][i] = data[i][j] | |
return data_trans | |
data = [['filename', 'date', '"numbers of errors"', '"numbers of successes"']] | |
files_names = glob.glob(os.path.join('folder/tmp', '*.log')) | |
output_file = 'folder/tmp/result.csv' | |
for filename in files_names: | |
with open(filename) as file: | |
lines = file.readlines() | |
num_err = 0 | |
num_suc = 0 | |
date = None | |
for line in lines: | |
if line.strip() == '': | |
continue | |
raw_data = line.strip().split('\t') | |
if date is None: | |
raw_date = raw_data[0] | |
date = '{}.{}.{}'.format(raw_date[0:4], raw_date[4:6].zfill(2), raw_date[6:8].zfill(2)) | |
code = int(raw_data[1]) | |
if code == 0: | |
num_suc += 1 | |
else: | |
num_err += 1 | |
data.append([filename, date, str(num_err), str(num_suc)]) | |
data_trans = transpose(data) | |
with open(output_file, 'w') as result: | |
rows = [] | |
for line in data_trans: | |
rows.append('\t'.join(line) + '\n') | |
result.writelines(rows) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment