How to Load and Save CSV and JSON Files in Python

A beginner-friendly data helper that loads or saves CSV and JSON files using only the Python standard library, with automatic format detection from the file extension.

Easy Python 3.9+ Aug 9, 2026 Modern tooling 13 views 0 copies

Python code

42 lines
Python 3.9+
from pathlib import Path
import json
import csv


def load_data(file_path):
    """Load CSV or JSON data from disk based on file extension."""
    path = Path(file_path)
    if path.suffix == ".json":
        with path.open() as f:
            return json.load(f)
    elif path.suffix == ".csv":
        with path.open(newline="") as f:
            return list(csv.DictReader(f))
    else:
        raise ValueError(f"Unsupported file type: {path.suffix}")


def save_data(file_path, data):
    """Save data to CSV or JSON file based on file extension."""
    path = Path(file_path)
    path.parent.mkdir(parents=True, exist_ok=True)
    if path.suffix == ".json":
        with path.open("w") as f:
            json.dump(data, f, indent=2)
    elif path.suffix == ".csv":
        if not data:
            raise ValueError("Cannot write empty list to CSV")
        fieldnames = data[0].keys()
        with path.open("w", newline="") as f:
            writer = csv.DictWriter(f, fieldnames=fieldnames)
            writer.writeheader()
            writer.writerows(data)
    else:
        raise ValueError(f"Unsupported file type: {path.suffix}")


if __name__ == "__main__":
    sample_data = [{"name": "Alice", "age": 30}, {"name": "Bob", "age": 25}]
    save_data("output/people.json", sample_data)
    loaded = load_data("output/people.json")
    print(loaded)

Output

stdout
[{'name': 'Alice', 'age': 30}, {'name': 'Bob', 'age': 25}]

How it works

The load_data function uses Path.suffix to inspect the file extension and then reads JSON with json.load or CSV as a list of dictionaries via csv.DictReader. save_data creates parent directories with mkdir(parents=True, exist_ok=True) and writes JSON with indentation or CSV using csv.DictWriter with a header row. The if __name__ == '__main__' guard lets the module be imported without executing the demo. Path.open uses a context manager, so files are closed automatically even if an error occurs.

Common mistakes

  • Using `json.loads` on a file object instead of `json.load` (or vice versa for strings)
  • Forgetting to pass `newline=''` when writing or reading CSV files, which can cause line-break issues
  • Assuming all CSV rows have the same keys; `DictWriter` uses the first row's keys, so missing keys cause `ValueError`

Variations

  1. Use `pandas.read_csv` and `pandas.read_json` for more powerful data manipulation.
  2. Use `pathlib.Path.read_text` and `json.loads` for JSON to read the entire file as a string first.

Real-world use cases

  • A script that reads user export files in either CSV or JSON format and converts them for processing.
  • A small CLI tool that saves configuration dictionaries as JSON with pretty-printing for readability.
  • A batch job that loads data from a CSV report, transforms it, and writes the result back to JSON for downstream services.

Sponsored

Run this sample

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

Open editor

More from Modern tooling

Related tutorials and quizzes for this topic.