Add kv_parse

kv_parse.py parses key=value pairs from logs and returns an dict per line
This commit is contained in:
TKE
2021-03-25 17:48:49 +01:00
parent fe90a3c686
commit 2537eeb951

24
kv_parse.py Executable file
View File

@@ -0,0 +1,24 @@
#!/usr/bin/env python3
import re
import json
import sys
with open(sys.argv[1],'rt') as infile:
data = infile.readlines()
kv_pat = re.compile('(?P<key>[^= ]+)=(?P<value>"[^"]+"|\S+)')
log=[]
for line in data:
line_dict={}
line = line.strip()
matches=kv_pat.findall(line)
for match in matches:
line_dict[match[0]] = match[1].strip('"')
log.append(line_dict)
print(json.dumps(log))
# with open('log.json','wt') as outfile:
# json.dump(log,outfile)