Add argparse to kv_parse
This commit is contained in:
13
kv_parse.py
13
kv_parse.py
@@ -1,10 +1,15 @@
|
|||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python3
|
||||||
import re
|
import re
|
||||||
import json
|
import json
|
||||||
|
import argparse
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
with open(sys.argv[1],'rt') as infile:
|
parser = argparse.ArgumentParser()
|
||||||
data = infile.readlines()
|
parser.add_argument('infile', nargs='?', type=argparse.FileType('r'), default=sys.stdin)
|
||||||
|
parser.add_argument('outfile', nargs='?', type=argparse.FileType('w'), default=sys.stdout)
|
||||||
|
args = parser.parse_args()
|
||||||
|
|
||||||
|
data = args.infile.readlines()
|
||||||
|
|
||||||
kv_pat = re.compile('(?P<key>[^= ]+)=(?P<value>"[^"]+"|\S+)')
|
kv_pat = re.compile('(?P<key>[^= ]+)=(?P<value>"[^"]+"|\S+)')
|
||||||
|
|
||||||
@@ -17,8 +22,6 @@ for line in data:
|
|||||||
line_dict[match[0]] = match[1].strip('"')
|
line_dict[match[0]] = match[1].strip('"')
|
||||||
log.append(line_dict)
|
log.append(line_dict)
|
||||||
|
|
||||||
print(json.dumps(log))
|
json.dump(log,args.outfile)
|
||||||
# with open('log.json','wt') as outfile:
|
|
||||||
# json.dump(log,outfile)
|
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user