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 <noreply@anthropic.com>
This commit is contained in:
@@ -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)
|
||||
Reference in New Issue
Block a user