File size: 1,609 Bytes
002f18c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
48
49
50
51
52
#!/usr/bin/env python3
"""Print the sum of the numeric values in the CSV column named 'Value'."""

from __future__ import annotations

import argparse
import csv
import sys
from decimal import Decimal, InvalidOperation
from pathlib import Path


def sum_value_column(csv_path: Path) -> Decimal:
    total = Decimal("0")

    with csv_path.open("r", encoding="utf-8-sig", newline="") as handle:
        reader = csv.DictReader(handle)
        if reader.fieldnames is None:
            raise ValueError("CSV file is empty or has no header row.")
        if "Value" not in reader.fieldnames:
            raise ValueError("CSV file must contain a column named 'Value'.")

        for line_number, row in enumerate(reader, start=2):
            raw_value = (row.get("Value") or "").strip()
            if not raw_value:
                continue
            try:
                total += Decimal(raw_value)
            except InvalidOperation as exc:
                raise ValueError(f"Invalid numeric value on line {line_number}: {raw_value!r}") from exc

    return total


def main() -> int:
    parser = argparse.ArgumentParser(description="Sum values from the 'Value' column in a CSV file.")
    parser.add_argument("csv_file", type=Path, help="Path to the input CSV file.")
    args = parser.parse_args()

    try:
        total = sum_value_column(args.csv_file)
    except (OSError, ValueError) as exc:
        print(f"Error: {exc}", file=sys.stderr)
        return 1

    print(total)
    return 0


if __name__ == "__main__":
    raise SystemExit(main())