Files
digest_app/process_food_data.py

55 lines
1.8 KiB
Python
Raw Permalink Normal View History

import openpyxl
# Read existing foods from Excel file
existing_foods = set()
try:
existing_workbook = openpyxl.load_workbook('formatted_data.xlsx')
existing_sheet = existing_workbook.active
for row in existing_sheet.iter_rows(min_row=2, max_col=1, max_row=existing_sheet.max_row):
existing_foods.add(row[0].value)
except FileNotFoundError:
# If the file does not exist, there are no existing foods
pass
# Read data from text file
with open('food_data.txt', 'r') as file:
data = file.readlines()
# Process the data
formatted_data = []
for line in data:
# Split each line by colon ':'
parts = line.strip().split(':')
# Check if there are two parts (food and ingredients)
if len(parts) == 2:
food = parts[0].strip()
ingredients = parts[1].strip()
# Check if the food is already in the existing foods set
if food not in existing_foods:
# If not, add the food to the set and to the formatted data list
existing_foods.add(food)
formatted_data.append((food, ingredients))
else:
# If only one part is present, add an empty string for ingredients
food = parts[0].strip()
if food not in existing_foods:
existing_foods.add(food)
formatted_data.append((food, ''))
# Write data to Excel file
workbook = openpyxl.Workbook()
sheet = workbook.active
# Add headers
sheet.cell(row=1, column=1).value = 'Food'
sheet.cell(row=1, column=2).value = 'Ingredients'
# Add data
for idx, entry in enumerate(formatted_data, start=2):
# Write food and ingredients to respective columns
sheet.cell(row=idx, column=1).value = entry[0]
sheet.cell(row=idx, column=2).value = entry[1]
# Save Excel file
workbook.save('formatted_data.xlsx')