-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathcsv-to-json.py
51 lines (43 loc) · 1.42 KB
/
csv-to-json.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
48
49
50
51
import argparse
import json
import csv
import sys
import re
assert sys.version_info >= (3, 6), "Python >= 3.6 is required"
def csv_to_dict(in_path):
d = {}
with open(in_path) as csvfile:
csv_reader = csv.reader(csvfile)
# Skip header
next(csv_reader)
for line_num, row in enumerate(csv_reader):
key = row[0]
text = row[1]
max_len = row[2]
# Ignore any columns after the third
if not key:
raise RuntimeError("Empty key detected at line: {}".format(line_num))
text = re.sub(
r'\{\{(\w+)\}\}',
r'{\1}',
text,
)
if key in d:
raise RuntimeError("Duplicate key {} at line: {}".format(key, line_num))
d[key] = text.strip()
return d
def write_json(d, out_path):
with open(out_path, 'w') as out_file:
json.dump(d, out_file, indent=2)
def parse_args():
p = argparse.ArgumentParser(description='translations: CSV to KEYVALUEJSON')
p.add_argument('--csv', metavar='PATH', help='path to the input CSV file', required=True)
p.add_argument('--json', metavar='PATH', help='json output path', required=True)
opt = p.parse_args()
return opt
def main():
opt = parse_args()
write_json(csv_to_dict(opt.csv), opt.json)
print("{} written".format(opt.json))
if __name__ == "__main__":
main()