How to Process CSV Data in Python with a Data Helper

Build a beginner-friendly data helper in Python that loads a CSV file, filters rows by a condition, and summarizes numeric fields.

Easy Python 3.9+ Aug 9, 2026 Data pipelines & processing 13 views 0 copies

Python code

36 lines
Python 3.9+
import csv
from pathlib import Path

DATA = [
    {"name": "Alice", "score": 88, "passed": True},
    {"name": "Bob", "score": 42, "passed": False},
    {"name": "Carol", "score": 95, "passed": True},
]


def load_csv(file_path: Path) -> list[dict]:
    with file_path.open(newline="", encoding="utf-8") as f:
        return list(csv.DictReader(f))


def filter_passed(records: list[dict]) -> list[dict]:
    return [r for r in records if r.get("passed") == "True"]


def summarize(records: list[dict]) -> dict:
    scores = [int(r["score"]) for r in records]
    return {"count": len(scores), "avg_score": round(sum(scores) / len(scores), 2)}


if __name__ == "__main__":
    tmp = Path("students.csv")
    with tmp.open("w", encoding="utf-8", newline="") as f:
        writer = csv.DictWriter(f, fieldnames=["name", "score", "passed"])
        writer.writeheader()
        writer.writerows(DATA)

    students = load_csv(tmp)
    passed = filter_passed(students)
    print("All records:", summarize(students))
    print("Passed records:", summarize(passed))
    tmp.unlink()

Output

stdout
All records: {'count': 3, 'avg_score': 75.0}
Passed records: {'count': 2, 'avg_score': 91.5}

How it works

The csv.DictReader converts each CSV row into a dictionary keyed by the header names, making field access readable. The filter_passed function uses a list comprehension with .get() to safely check the passed column, comparing against the string 'True' since CSV stores everything as text. The summarize function converts score strings to integers and computes the average, rounding to two decimal places. The if __name__ == "__main__" guard keeps the test data creation and output separate from the reusable helper functions.

Common mistakes

  • Forgetting that CSV values are strings; compare against 'True' not True
  • Hardcoding file paths instead of using Path and context managers
  • Not handling empty rows or missing keys with .get()
  • Mixing functions with side effects and pure logic in the same block

Variations

  1. Use pandas.read_csv for larger datasets and built-in filtering
  2. Return a generator from load_csv to stream rows instead of loading all at once

Real-world use cases

  • ETL jobs that ingest flat files, filter valid entries, and produce aggregate stats for dashboards.
  • Batch processing of survey or log exports where only rows meeting a status flag are analyzed.
  • Building a lightweight reporting script that summarizes weekly sales data without a full database.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from Data pipelines & processing

Related tutorials and quizzes for this topic.