From 837c4bb1a1f594e71ad47f38fdcce946bcafbc5b Mon Sep 17 00:00:00 2001 From: Matthew Giblett Date: Tue, 5 Nov 2024 20:45:00 +1000 Subject: [PATCH] feat: add save_csv function --- serps/main.py | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/serps/main.py b/serps/main.py index 2545e15..b89bab1 100644 --- a/serps/main.py +++ b/serps/main.py @@ -1,3 +1,4 @@ +import csv import os from pathlib import Path from typing import Any @@ -25,6 +26,15 @@ def save_excel(file_path: Path | str, data: DataFrame) -> None: data.to_excel(writer) +def save_csv(output_dir: Path, output: list[dict]) -> None: + if len(output) == 0: + print(f"Failed to save {output_dir}. Output empty.") + return + with open(output_dir, "w") as csv_file: + writer = csv.DictWriter(csv_file, fieldnames=output[0].keys()) + writer.writeheader() + writer.writerows(output) + def load_list(file_path: Path | str) -> dict[str, Any] | None: if not file_path: return