Spaces:
Running
Running
import json | |
import re | |
def process_json_files(start, end): | |
base_path = "texts" | |
results = [] | |
for i in range(start, end + 1): | |
file_name = f"{base_path}/{i:02}.json" | |
try: | |
with open(file_name, 'r', encoding='utf-8') as file: | |
data = json.load(file) | |
if data: | |
# Return a tuple of book_id and text data | |
results.append((i, {"title": data.get("title", "No title"), "text": data.get("text", [])})) | |
except FileNotFoundError: | |
results.append((i, {"error": f"File {file_name} not found."})) # Use a tuple here | |
except json.JSONDecodeError as e: | |
results.append((i, {"error": f"File {file_name} could not be read as JSON: {e}"})) # Use a tuple here | |
except KeyError as e: | |
results.append((i, {"error": f"Expected key 'text' is missing in {file_name}: {e}"})) # Use a tuple here | |
return results |