28 lines
759 B
Python
28 lines
759 B
Python
|
|
"""Shared CSV helpers."""
|
||
|
|
|
||
|
|
from __future__ import annotations
|
||
|
|
|
||
|
|
import csv
|
||
|
|
from pathlib import Path
|
||
|
|
|
||
|
|
|
||
|
|
def read_csv_rows(path: Path) -> list[dict[str, str]]:
|
||
|
|
text = path.read_text(encoding="utf-8-sig")
|
||
|
|
reader = csv.DictReader(text.splitlines())
|
||
|
|
return [{k.strip(): (v or "").strip() for k, v in row.items() if k} for row in reader]
|
||
|
|
|
||
|
|
|
||
|
|
def pick(row: dict[str, str], *names: str) -> str:
|
||
|
|
lowered = {k.lower(): v for k, v in row.items()}
|
||
|
|
for name in names:
|
||
|
|
value = lowered.get(name.lower())
|
||
|
|
if value:
|
||
|
|
return value
|
||
|
|
return ""
|
||
|
|
|
||
|
|
|
||
|
|
def parse_amount(value: str) -> float:
|
||
|
|
cleaned = value.replace("€", "").replace("EUR", "").replace(",", ".").strip()
|
||
|
|
if not cleaned:
|
||
|
|
return 0.0
|
||
|
|
return float(cleaned)
|