This repository has been archived by the owner on Jun 12, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjson_tbtl.py
47 lines (37 loc) · 1.59 KB
/
json_tbtl.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
import argparse
import json
parser = argparse.ArgumentParser()
parser.add_argument("input", help="input file to be parsed")
parser.add_argument("--headers", help="write names of column headers, separated by commas", type=str)
args = parser.parse_args()
def create_tbtl_data(data: list, ordered_headers: list) -> str:
field_lengths = get_field_lengths(data, ordered_headers)
return format_parsed_data(data, ordered_headers, field_lengths)
def get_field_lengths(data: list, headers: list) -> dict:
field_data = {field: len(field) for field in headers}
for entry in data:
for key, value in entry.items():
if value is not None and len(str(value)) > field_data[key]:
field_data[key] = len(str(value))
return field_data
def format_parsed_data(data: list, headers: list, lengths: dict) -> str:
entries = []
header_line = ""
for header in headers:
header_line += header.upper().ljust(lengths[header]) + ' '
entries.append(header_line.strip())
for entry in data:
line = ""
for header in headers:
if entry[header] is not None:
line += str(entry[header]).ljust(lengths[header]) + ' '
else:
line += ' ' * lengths[header] + ' '
entries.append(line.strip())
return '\n'.join(entry for entry in entries)
if __name__ == "__main__":
with open(args.input, 'r') as f:
parsed_data = json.load(f)
ordered_headers = [header.strip() for header in args.headers.split(',')]
tbtl_data = create_tbtl_data(parsed_data, ordered_headers)
print(tbtl_data)