-
Notifications
You must be signed in to change notification settings - Fork 0
/
csv2json.py
31 lines (23 loc) · 870 Bytes
/
csv2json.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
from modules import mapper
import json, sys
# constants, make these configurable via options
FIELD_SEPARATOR = ','
SCHEMA_DELIM = '.'
if len(sys.argv) != 3:
print("You must provide an input and output file! Usage: csv2json.py <inputfile> <outputfile>")
sys.exit()
input_file_path = sys.argv[1]
output_file_path = sys.argv[2]
with open(input_file_path, 'r') as input_file:
lines = input_file.read().splitlines()
# the first line is the schema for the entire file
schema = lines[0].split(FIELD_SEPARATOR)
data = []
for row in lines[1:]:
data.append(mapper.parse_row(schema, row.split(FIELD_SEPARATOR), SCHEMA_DELIM))
with open(output_file_path, 'w') as output_file:
output_file.write(json.dumps(data, indent=2))
if output_file.closed:
print("All data successfully mapped!")
else:
print("An error occurred, please check your data and schema!")