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.
Python code
25 linesimport 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
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
- Use `csv.reader` and map column names manually for more control.
- 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
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.