From dc2382fb00f8a2cc4c7bdd4ebe25e8d5228c8f5f Mon Sep 17 00:00:00 2001 From: Davide Grilli Date: Mon, 13 Jul 2026 22:44:32 +0200 Subject: [PATCH] Aggiunge script di conversione voltammetrie CHI660C in CSV europeo Rimuove metadati/risultati dai .txt e produce CSV a due colonne (Potential/V, Current/A) con separatore ';' e decimale ','. Co-Authored-By: Claude Sonnet 5 --- .gitignore | 8 ++++++++ cv_to_csv.py | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 .gitignore create mode 100644 cv_to_csv.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..698d8c1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,8 @@ +__pycache__/ +*.pyc +.venv/ +venv/ +.ipynb_checkpoints/ + +*.txt +*.csv diff --git a/cv_to_csv.py b/cv_to_csv.py new file mode 100644 index 0000000..58f0746 --- /dev/null +++ b/cv_to_csv.py @@ -0,0 +1,56 @@ +"""Converte i file .txt di output CHI660C (voltammetria ciclica) in CSV europeo. + +Rimuove i metadati iniziali e la sezione "Results" e mantiene solo le due +colonne dati (Potential/V, Current/A), scritte con separatore ';' e +virgola come separatore decimale. +""" + +import csv +import sys +from pathlib import Path + +DATA_HEADER = "Potential/V, Current/A" + + +def convert_file(input_path: Path, output_path: Path) -> None: + with input_path.open("r", encoding="utf-8", errors="replace") as f: + lines = f.readlines() + + try: + header_idx = next(i for i, line in enumerate(lines) if line.strip() == DATA_HEADER) + except StopIteration: + raise ValueError(f"Intestazione dati '{DATA_HEADER}' non trovata in {input_path}") + + rows = [] + for line in lines[header_idx + 1:]: + line = line.strip() + if not line: + continue + potential, current = (v.strip() for v in line.split(",", 1)) + rows.append((potential.replace(".", ","), current.replace(".", ","))) + + with output_path.open("w", encoding="utf-8", newline="") as f: + writer = csv.writer(f, delimiter=";") + writer.writerow(["Potential/V", "Current/A"]) + writer.writerows(rows) + + +def main(argv: list[str]) -> None: + folder = Path(argv[1]) if len(argv) > 1 else Path(__file__).parent + txt_files = sorted(folder.glob("*.txt")) + + if not txt_files: + print(f"Nessun file .txt trovato in {folder}") + return + + for txt_file in txt_files: + csv_file = txt_file.with_suffix(".csv") + try: + convert_file(txt_file, csv_file) + print(f"OK {txt_file.name} -> {csv_file.name}") + except ValueError as e: + print(f"SKIP {txt_file.name}: {e}") + + +if __name__ == "__main__": + main(sys.argv)