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.
Python code
32 linesimport 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
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
- Use `pandas.read_csv` for automatic type inference and DataFrame manipulation.
- 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
More from ML engineering pipelines
- Bayesian Optimization in Python: A Simplified Mock Implementation medium
- Build a Data Helper Class in Python for ML Pipelines easy
- Build a Mock Random Forest Classifier in Python easy
- Champion Challenger Deployment Mock in Python easy
- Compare Model A vs Model B Metrics in Python easy
- Create a Minimal Great Expectations Suite Mock in Python easy
Keep learning
Related tutorials and quizzes for this topic.