import pandas as pd

# Define input and output paths
ods_path = 'RusFraLists9000.csv'  # The expected file name

# Read ODS file
ods = pd.read_csv(ods_path)

# Process and split into sections
sections = {}
current_title = None
for _, row in ods.iterrows():
    if str(row["c3"]).strip() == "##":
        current_title = str(row["c2"]).strip()
        sections[current_title] = []
    elif current_title and pd.notna(row["c2"]) and pd.notna(row["c3"]):
        sections[current_title].append(f"{row['c2'].strip()} ; {row['c3'].strip()}")

# Write each section to a separate .txt file
for title, entries in sections.items():
    filename = f"{title}.txt"
    safe_filename = filename.lower().replace("/", "_").replace(",", " ").replace("—", " ").replace("\\", "_").replace(":", "_").replace("  ", " ").replace(" ", "-")
    with open(safe_filename, "w", encoding="utf-8") as f:
        f.write(f"====== {title} ======\n\n")
        f.write("\n\n".join(entries))

# List created files
output_files = sorted(os.listdir(output_dir))
output_files[:10]  # Show first 10 for brevity
