Create a Data Helper Class for Beginners in Python

A simple Python class to read and write JSON and CSV files from a local directory, ideal for automating data workflows in cloud environments.

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

Python code

46 lines
Python 3.9+
import json
from pathlib import Path

class DataHelper:
    """Simple helper for reading and writing common data files."""
    
    def __init__(self, directory="data"):
        self.directory = Path(directory)
        self.directory.mkdir(exist_ok=True)
    
    def save_json(self, filename, data):
        filepath = self.directory / f"{filename}.json"
        with filepath.open("w") as f:
            json.dump(data, f, indent=2)
        return filepath
    
    def load_json(self, filename):
        filepath = self.directory / f"{filename}.json"
        with filepath.open("r") as f:
            return json.load(f)
    
    def save_csv(self, filename, headers, rows):
        filepath = self.directory / f"{filename}.csv"
        with filepath.open("w") as f:
            f.write(",".join(headers) + "\n")
            for row in rows:
                f.write(",".join(str(item) for item in row) + "\n")
        return filepath

if __name__ == "__main__":
    helper = DataHelper()
    
    users = [{"id": 1, "name": "Alice", "role": "admin"}, 
             {"id": 2, "name": "Bob", "role": "user"}]
    
    json_path = helper.save_json("users", users)
    loaded = helper.load_json("users")
    
    csv_path = helper.save_csv("logs", 
                               ["timestamp", "level", "message"],
                               [["2024-01-01", "INFO", "Started"],
                                ["2024-01-01", "WARN", "Retry"]])
    
    print(f"JSON saved: {json_path}")
    print(f"JSON loaded: {loaded}")
    print(f"CSV saved: {csv_path}")

Output

stdout
JSON saved: data/users.json
JSON loaded: [{'id': 1, 'name': 'Alice', 'role': 'admin'}, {'id': 2, 'name': 'Bob', 'role': 'user'}]
CSV saved: data/logs.csv

How it works

The DataHelper class encapsulates common file I/O operations, making it easy to manage data locally before uploading to cloud storage. It uses pathlib.Path for cross-platform path handling and automatically creates the target directory on initialization. JSON methods use the standard json module with pretty-print indentation for readability. The CSV writer manually joins fields with commas—ideal for simple data but not edge cases like embedded commas. This pattern is a building block for scripts that fetch, process, and store data in cloud pipelines.

Common mistakes

  • Forgetting to close files—use a `with` statement to auto-close.
  • Assuming the directory exists; `mkdir(exist_ok=True)` prevents errors.
  • Mixing up `json.dump` for files and `json.dumps` for strings.
  • Hardcoding absolute paths instead of using relative paths for portability.

Variations

  1. Use `csv.DictWriter` to write CSV from dictionary rows with headers automatically.
  2. Add a `load_csv` method using `csv.reader` to read back CSV data.

Real-world use cases

  • Local data staging in an ETL job before uploading to cloud storage like S3.
  • Automated reporting scripts that save and cache API responses as JSON for later analysis.
  • Configuration management for cloud functions that read settings from a JSON file on startup.

Sponsored

Run this sample

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

Open editor

More from Cloud + Python

Related tutorials and quizzes for this topic.