-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcsv-to-json.py
executable file
·67 lines (46 loc) · 2.04 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
#!/usr/bin/env python
# This is written with python3 syntax
import csv
import os
import json
INPUT_FILE_NAME = 'lookup.csv'
INPUT_FILE_PATH = os.path.join(os.getcwd(), INPUT_FILE_NAME)
CSV_DELIMITER = ','
OUTPUT_FILE_NAME = 'lookup.json'
OUTPUT_FILE_PATH = os.path.join(os.getcwd(), OUTPUT_FILE_NAME)
LOOKUP_COL = "lookup_id"
# Each CSV line will be converted into a dictionary object, and pushed
# onto an array. This ensures that the generated json
# will have the same order as the lines in the CSV file.
array_of_ordered_dict = []
# function to convert the CSV into an array that contains a json-like
# dictionary for each line in the CSV file
def create_ordered_dict_from_input():
with open(INPUT_FILE_PATH) as csv_file:
csv_reader = csv.DictReader(csv_file, delimiter=CSV_DELIMITER)
print("Reading %s" % INPUT_FILE_PATH)
for row in csv_reader:
array_of_ordered_dict.append(row)
print("Finished reading %s" % INPUT_FILE_PATH)
return array_of_ordered_dict;
# Convert the array of dictionary objects into a json object.
def convert_array_of_ordered_dict_to_json(array_of_ordered_dict):
print("Creating %s" % OUTPUT_FILE_PATH)
f = open(OUTPUT_FILE_PATH, "w")
# Create the json lookup table
f.write("{\n")
arr_len = len(array_of_ordered_dict)
for idx, row in enumerate(array_of_ordered_dict):
lookup_id = row[LOOKUP_COL]
del row[LOOKUP_COL]
# lookup_id is a dictionary key, with a json dict as the value
json_element = '"{0}" : {1}'.format(lookup_id, json.dumps(row))
# If this is the last json element, then the dictionary should be closed rather than
# adding a trailing comma.
json_line = ''.join([json_element, "\n}\n"]) if (idx+1) == arr_len else ''.join([json_element, ",\n"])
f.write(json_line)
print("Finished writing %s" % OUTPUT_FILE_PATH)
return 0
if __name__ == "__main__":
array_of_ordered_dict = create_ordered_dict_from_input()
convert_array_of_ordered_dict_to_json(array_of_ordered_dict)