-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_parser.py
More file actions
47 lines (32 loc) · 1.48 KB
/
csv_parser.py
File metadata and controls
47 lines (32 loc) · 1.48 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""Lightweight CSV parser with filtering and aggregation helpers."""
import csv
import io
from typing import Any, Callable, Iterator
def read_csv(filepath: str, delimiter: str = ",") -> list[dict]:
with open(filepath, newline="", encoding="utf-8") as f:
return list(csv.DictReader(f, delimiter=delimiter))
def write_csv(filepath: str, rows: list[dict], fieldnames: list[str] | None = None) -> None:
if not rows:
return
fields = fieldnames or list(rows[0].keys())
with open(filepath, "w", newline="", encoding="utf-8") as f:
writer = csv.DictWriter(f, fieldnames=fields)
writer.writeheader()
writer.writerows(rows)
def filter_rows(rows: list[dict], predicate: Callable[[dict], bool]) -> list[dict]:
return [r for r in rows if predicate(r)]
def pluck(rows: list[dict], *keys: str) -> list[dict]:
return [{k: r[k] for k in keys if k in r} for r in rows]
def group_by(rows: list[dict], key: str) -> dict[str, list[dict]]:
groups: dict[str, list[dict]] = {}
for row in rows:
groups.setdefault(row.get(key, ""), []).append(row)
return groups
def aggregate(rows: list[dict], key: str, numeric_col: str) -> dict[str, float]:
groups = group_by(rows, key)
return {
g: sum(float(r[numeric_col]) for r in rs if r.get(numeric_col))
for g, rs in groups.items()
}
def from_string(text: str, delimiter: str = ",") -> list[dict]:
return list(csv.DictReader(io.StringIO(text), delimiter=delimiter))