Load CSV Training Data Without Pandas in Python

This code loads a CSV file into a list of dictionaries using only the standard library, ideal for small ML training data without heavy dependencies.

Easy Python 3.6+ Aug 9, 2026 ML engineering pipelines 13 views 0 copies

Python code

25 lines
Python 3.6+
import csv
from pathlib import Path

def load_csv(path):
    """Load CSV file into list of dicts without pandas."""
    rows = []
    with open(path, newline='', encoding='utf-8') as f:
        reader = csv.DictReader(f)
        for row in reader:
            rows.append(dict(row))
    return rows

if __name__ == "__main__":
    data_path = Path("mock_training_data.csv")
    data_path.write_text(
        "feature1,feature2,label\n"
        "1.2,3.4,0\n"
        "5.6,7.8,1\n"
        "9.1,2.2,0\n"
    )
    data = load_csv(data_path)
    print(f"Loaded {len(data)} rows")
    for row in data:
        print(row)
    data_path.unlink()

Output

stdout
Loaded 3 rows
{'feature1': '1.2', 'feature2': '3.4', 'label': '0'}
{'feature1': '5.6', 'feature2': '7.8', 'label': '1'}
{'feature1': '9.1', 'feature2': '2.2', 'label': '0'}

How it works

The csv.DictReader reads each row as a dictionary with keys from the header row. Using dict(row) converts the OrderedDict to a plain dict. The newline='' parameter ensures proper handling of line endings across platforms. Encoding is set to UTF-8 for broad compatibility. This approach avoids pandas overhead for small datasets.

Common mistakes

  • Forgetting `newline=''` when opening the file, which can cause extra blank lines on Windows.
  • Assuming values are automatically converted to floats or ints; they remain strings.
  • Not closing the file if using `open` without a context manager.
  • Ignoring empty lines at the end of the file, which can be handled by skipping in a loop.

Variations

  1. Use `csv.reader` and map column names manually for more control.
  2. Use `pathlib.Path.read_text` with `csv.reader` for a single-line file read.

Real-world use cases

  • Loading a small static dataset for model inference in a serverless function without adding pandas to the bundle.
  • Reading training features from a CSV exported by a labeling tool in a quick prototype script.
  • Parsing configuration-like CSV files in a data pipeline where pandas is not installed.

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.