-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcsv_to_json.py
71 lines (51 loc) · 2.21 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
68
69
70
71
import csv
import json
def find_string_indices(substring, text):
# Case-insensitive search for the substring in the text
index = text.lower().find(substring.lower())
if index != -1:
# If the substring is found, return start and end indices
start_index = index
end_index = start_index + len(substring)
return start_index, end_index
else:
# If the substring is not found, return None
return None, None
def csv_to_json(csv_file_path, json_file_path):
result_array = []
with open(csv_file_path, "r") as csv_file:
csv_reader = csv.reader(csv_file, delimiter=";")
header = next(csv_reader) # Assuming the first row is the header
for row in csv_reader:
try:
raw_ing = row[0].strip()
qty = row[2].strip()
uom = row[3].strip()
name = row[1].strip()
name_start, name_stop = find_string_indices(name, raw_ing)
row_array = [raw_ing]
index_array = []
if (
name != None
and name != ""
and name_start != "-"
and name_stop != "-"
):
index_array.append([int(name_start), int(name_stop), "INGREDIENT"])
if qty != None and qty != "" and qty != "-":
qty_start, qty_end = find_string_indices(qty, raw_ing)
index_array.append([int(qty_start), int(qty_end), "QUANTITY"])
if uom != None and uom != "" and uom != "-":
uom_start, uom_end = find_string_indices(uom, raw_ing)
index_array.append([int(uom_start), int(uom_end), "UNIT"])
row_array.append(index_array)
result_array.append(row_array)
except:
print("failed to parse {}".format(raw_ing))
# Writing the JSON array to a file
with open(json_file_path, "w") as json_file:
json.dump(result_array, json_file, indent=2)
# Example usage
csv_file_path = "Answer 4.csv"
json_file_path = "ing_new.json"
csv_to_json(csv_file_path, json_file_path)