File size: 847 Bytes
4c895ac |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
import json
# Open the input and output files
with open('/home/yiyangai/stephenqs/datasets/wikihow/WikihowText_data.json', 'r', encoding='utf-8') as infile, open('/home/yiyangai/stephenqs/datasets/wikihow/Computers and Electronics.json', 'w', encoding='utf-8') as outfile:
for line in infile:
try:
# Parse the JSON object from the current line
item = json.loads(line.strip())
# Apply filtering condition
if item.get("category_hierarchy") and item["category_hierarchy"][0] == "Computers and Electronics":
# Write the JSON object as a single line to the output file
outfile.write(json.dumps(item, ensure_ascii=False) + '\n')
except json.JSONDecodeError as e:
print(f"Skipping invalid JSON object: {e}")
print("Extraction complete.")
|