How to Load CSV Training Data in Python Without Pandas

Load CSV training data using Python's standard library and mock it with io.StringIO for testing, returning headers and rows as dictionaries.

Easy Python 3.9+ Aug 9, 2026 ML engineering pipelines 14 views 0 copies

Python code

32 lines
Python 3.9+
import csv
from pathlib import Path


def load_csv_training_data(file_path: str | Path) -> tuple[list[str], list[dict[str, str]]]:
    """Load CSV training data and return headers plus rows as dictionaries."""
    with open(file_path, mode="r", newline="", encoding="utf-8") as csv_file:
        reader = csv.DictReader(csv_file)
        headers = reader.fieldnames or []
        rows = [dict(row) for row in reader]
    return headers, rows


if __name__ == "__main__":
    # Mock training data without writing a file: use io.StringIO instead.
    import io

    mock_csv = """age,income,label
25,50000,0
32,72000,1
41,91000,1
"""

    mock_file = io.StringIO(mock_csv)
    reader = csv.DictReader(mock_file)
    headers = reader.fieldnames or []
    rows = [dict(row) for row in reader]

    print("Headers:", headers)
    print("Rows:")
    for row in rows:
        print(row)

Output

stdout
Headers: ['age', 'income', 'label']
Rows:
{'age': '25', 'income': '50000', 'label': '0'}
{'age': '32', 'income': '72000', 'label': '1'}
{'age': '41', 'income': '91000', 'label': '1'}

How it works

Using csv.DictReader converts each row into a dictionary keyed by the header names, making field access intuitive. The fieldnames attribute provides the headers; if missing, it defaults to an empty list. io.StringIO wraps a string so it behaves like a file object, allowing you to test CSV parsing without writing to disk. Returning (headers, rows) keeps the data structured for pipeline use. Encoding is set to UTF-8 to handle common text data.

Common mistakes

  • Forgetting `newline=''` when opening the file, which can cause extra blank lines on Windows.
  • Assuming all values are numeric; CSV fields are strings until explicitly converted.
  • Not handling missing headers when the CSV file is empty.
  • Overlooking that `csv.DictReader` reads rows as strings, so type conversion is needed before model training.

Variations

  1. Use `pandas.read_csv` for automatic type inference and DataFrame manipulation.
  2. Read the file line by line with `csv.reader` and manually zip headers to rows for more control.

Real-world use cases

  • Loading small CSV datasets in lightweight ML pipelines where pandas is not installed or too heavy.
  • Parsing CSV test fixtures in unit tests to verify model training functions.
  • Ingesting CSV exports from legacy systems into a data preprocessing step without external dependencies.

Sponsored

Run this sample

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

Open editor

More from ML engineering pipelines

Related tutorials and quizzes for this topic.