Aggiunge normalizzazione del potenziale rispetto a un riferimento
Chiede all'utente il potenziale di riferimento e aggiunge una colonna Potential normalized/V (= Potential/V - riferimento) ai file *_ultimo_ciclo.csv, sovrascrivendoli. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,61 @@
|
|||||||
|
"""Aggiunge ai CSV dell'ultimo ciclo una colonna di potenziale normalizzato.
|
||||||
|
|
||||||
|
Chiede all'utente il potenziale di riferimento (in V) e aggiunge, per ogni
|
||||||
|
file *_ultimo_ciclo.csv nella cartella, una terza colonna pari a
|
||||||
|
Potential/V - riferimento. I file vengono sovrascritti.
|
||||||
|
"""
|
||||||
|
|
||||||
|
import csv
|
||||||
|
import sys
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
INPUT_SUFFIX = "_ultimo_ciclo"
|
||||||
|
|
||||||
|
|
||||||
|
def ask_reference_potential() -> float:
|
||||||
|
while True:
|
||||||
|
raw = input("Potenziale di riferimento (V): ").strip()
|
||||||
|
try:
|
||||||
|
return float(raw)
|
||||||
|
except ValueError:
|
||||||
|
print("Valore non valido, inserisci un numero (es. -0.152).")
|
||||||
|
|
||||||
|
|
||||||
|
def normalize_file(path: Path, reference: float) -> None:
|
||||||
|
with path.open("r", encoding="utf-8", newline="") as f:
|
||||||
|
reader = csv.reader(f, delimiter=";")
|
||||||
|
header = next(reader)
|
||||||
|
rows = [(row[0], row[1]) for row in reader]
|
||||||
|
|
||||||
|
header = [*header, "Potential normalized/V"]
|
||||||
|
new_rows = []
|
||||||
|
for potential_str, current_str in rows:
|
||||||
|
potential = float(potential_str.replace(",", "."))
|
||||||
|
normalized = potential - reference
|
||||||
|
new_rows.append((potential_str, current_str, f"{normalized:.3f}".replace(".", ",")))
|
||||||
|
|
||||||
|
with path.open("w", encoding="utf-8", newline="") as f:
|
||||||
|
writer = csv.writer(f, delimiter=";")
|
||||||
|
writer.writerow(header)
|
||||||
|
writer.writerows(new_rows)
|
||||||
|
|
||||||
|
|
||||||
|
def main(argv: list[str]) -> None:
|
||||||
|
folder = Path(argv[1]) if len(argv) > 1 else Path(__file__).parent
|
||||||
|
csv_files = sorted(
|
||||||
|
p for p in folder.glob("*.csv") if p.stem.endswith(INPUT_SUFFIX)
|
||||||
|
)
|
||||||
|
|
||||||
|
if not csv_files:
|
||||||
|
print(f"Nessun file *{INPUT_SUFFIX}.csv trovato in {folder}")
|
||||||
|
return
|
||||||
|
|
||||||
|
reference = ask_reference_potential()
|
||||||
|
|
||||||
|
for csv_file in csv_files:
|
||||||
|
normalize_file(csv_file, reference)
|
||||||
|
print(f"OK {csv_file.name} (potenziale normalizzato aggiunto)")
|
||||||
|
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
main(sys.argv)
|
||||||
Reference in New Issue
Block a user