-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlog_parser.py
47 lines (29 loc) · 1.06 KB
/
log_parser.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#! /usr/bin/python3
import argparse, re
def parse_record(record):
regex = r"^\*(?P<record_type>\w*):(?P<record_data>.*)$"
matches = re.finditer(regex, test_str, re.MULTILINE)
data_sources = []
for idx, match in enumerate(matches):
data_source, data = matches.groups()
def extract_records(data):
regex = r"(?P<record>.*?)^-"
matches = re.finditer(regex, test_str, re.MULTILINE | re.DOTALL)
records = []
for idx, record in enumerate(matches):
records.append(record)
return record
def parse_log(input_file, output_folder):
with open(input_file, 'r') as infile:
data = infile.read()
records = extract_records(data)
for record in records:
parse_record(record)
if __name__ == '__main__':
parser = argparse,ArgumentParser()
parser.add_argument(
'input_file', type=str, requred=True, help='The input file')
parser.add_argument(
'--output-folder', type=str, help='The output folder')
args = parser.parse_args()
parse_log(args.input_file, args.output_folder)