File size: 885 Bytes
4c895ac |
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 |
import json
filtered_data = []
# Load the JSON file content as a single string
with open('/home/yiyangai/stephenqs/datasets/wikihow/WikihowText_data.json', 'r', encoding='utf-8') as f:
content = f.read()
# Split the content into individual JSON objects
json_objects = content.splitlines()
# Process each JSON object individually
for obj in json_objects:
try:
item = json.loads(obj.strip())
if item.get("category_hierarchy") and item["category_hierarchy"][0] == "Finance and Business":
filtered_data.append(item)
except json.JSONDecodeError as e:
print(f"Skipping invalid JSON object: {e}")
# Save the filtered data to a new JSON file
with open('Arts and Entertainment.json', 'w', encoding='utf-8') as f:
json.dump(filtered_data, f, ensure_ascii=False, indent=4)
print(f"{len(filtered_data)} items extracted to 'output.json'")
|