|
| 1 | +import csv |
| 2 | +import json |
| 3 | + |
| 4 | + |
| 5 | +def find_string_indices(substring, text): |
| 6 | + # Case-insensitive search for the substring in the text |
| 7 | + index = text.lower().find(substring.lower()) |
| 8 | + |
| 9 | + if index != -1: |
| 10 | + # If the substring is found, return start and end indices |
| 11 | + start_index = index |
| 12 | + end_index = start_index + len(substring) |
| 13 | + return start_index, end_index |
| 14 | + else: |
| 15 | + # If the substring is not found, return None |
| 16 | + return None, None |
| 17 | + |
| 18 | + |
| 19 | +def csv_to_json(csv_file_path, json_file_path): |
| 20 | + result_array = [] |
| 21 | + |
| 22 | + with open(csv_file_path, "r") as csv_file: |
| 23 | + csv_reader = csv.reader(csv_file, delimiter=";") |
| 24 | + |
| 25 | + header = next(csv_reader) # Assuming the first row is the header |
| 26 | + |
| 27 | + for row in csv_reader: |
| 28 | + try: |
| 29 | + raw_ing = row[0].strip() |
| 30 | + |
| 31 | + qty = row[2].strip() |
| 32 | + |
| 33 | + uom = row[3].strip() |
| 34 | + |
| 35 | + name = row[1].strip() |
| 36 | + name_start, name_stop = find_string_indices(name, raw_ing) |
| 37 | + |
| 38 | + row_array = [raw_ing] |
| 39 | + |
| 40 | + index_array = [] |
| 41 | + |
| 42 | + if ( |
| 43 | + name != None |
| 44 | + and name != "" |
| 45 | + and name_start != "-" |
| 46 | + and name_stop != "-" |
| 47 | + ): |
| 48 | + index_array.append([int(name_start), int(name_stop), "INGREDIENT"]) |
| 49 | + |
| 50 | + if qty != None and qty != "" and qty != "-": |
| 51 | + qty_start, qty_end = find_string_indices(qty, raw_ing) |
| 52 | + index_array.append([int(qty_start), int(qty_end), "QUANTITY"]) |
| 53 | + |
| 54 | + if uom != None and uom != "" and uom != "-": |
| 55 | + uom_start, uom_end = find_string_indices(uom, raw_ing) |
| 56 | + index_array.append([int(uom_start), int(uom_end), "UNIT"]) |
| 57 | + |
| 58 | + row_array.append(index_array) |
| 59 | + result_array.append(row_array) |
| 60 | + except: |
| 61 | + print("failed to parse {}".format(raw_ing)) |
| 62 | + |
| 63 | + # Writing the JSON array to a file |
| 64 | + with open(json_file_path, "w") as json_file: |
| 65 | + json.dump(result_array, json_file, indent=2) |
| 66 | + |
| 67 | + |
| 68 | +# Example usage |
| 69 | +csv_file_path = "Answer 4.csv" |
| 70 | +json_file_path = "ing_new.json" |
| 71 | +csv_to_json(csv_file_path, json_file_path) |
0 commit comments