-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathremove_empty_recipes.py
More file actions
60 lines (45 loc) · 1.8 KB
/
remove_empty_recipes.py
File metadata and controls
60 lines (45 loc) · 1.8 KB
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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
删除所需食材为空的菜谱
"""
import json
from pathlib import Path
def remove_empty_recipes(input_file, output_file=None):
"""删除食材为空的菜谱"""
if output_file is None:
output_file = input_file
print(f"正在读取文件: {input_file}")
# 读取JSON文件
with open(input_file, 'r', encoding='utf-8') as f:
recipes = json.load(f)
print(f"原始菜谱数量: {len(recipes)}")
# 过滤掉食材为空的菜谱
filtered_recipes = []
removed_count = 0
for recipe in recipes:
# 检查是否有任何食材
main_ingredients = recipe.get('main_ingredients', [])
auxiliary_ingredients = recipe.get('auxiliary_ingredients', [])
seasonings = recipe.get('seasonings', [])
# 检查是否所有食材数组都为空
has_ingredients = (
(main_ingredients and len(main_ingredients) > 0) or
(auxiliary_ingredients and len(auxiliary_ingredients) > 0) or
(seasonings and len(seasonings) > 0)
)
if has_ingredients:
filtered_recipes.append(recipe)
else:
removed_count += 1
print(f"删除菜谱: {recipe.get('name', '未知')} (来源: {recipe.get('source_file', '未知')})")
print(f"\n删除的菜谱数量: {removed_count}")
print(f"保留的菜谱数量: {len(filtered_recipes)}")
# 保存过滤后的数据
print(f"\n正在保存到: {output_file}")
with open(output_file, 'w', encoding='utf-8') as f:
json.dump(filtered_recipes, f, ensure_ascii=False, indent=2)
print("✓ 完成!")
if __name__ == '__main__':
input_file = 'recipes_parsed.json'
remove_empty_recipes(input_file)