neuralworm's picture
add util
382e2e7
raw
history blame
905 Bytes
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:
results.append({
"book": i,
"title": data.get("title", "No title"),
"text": data.get("text", "No text"),
})
except FileNotFoundError:
results.append({"error": f"File {file_name} not found."})
except json.JSONDecodeError as e:
results.append({"error": f"File {file_name} could not be read as JSON: {e}"})
except KeyError as e:
results.append({"error": f"Expected key 'text' is missing in {file_name}: {e}"})
return results