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.
Python code
46 linesimport 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
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
- Use `csv.DictWriter` to write CSV from dictionary rows with headers automatically.
- 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
More from Cloud + Python
- Build a URL Shortener Client with Python medium
- Create a Cloud Storage Helper Class in Python easy
- Cross Account Role Chaining Mock Credentials in Python medium
- Exponential Backoff with Jitter for Cloud API Calls in Python medium
- Generate Mock CloudFormation Stack Events in Python easy
- Generate a Mock Presigned URL in Python with HMAC medium
Keep learning
Related tutorials and quizzes for this topic.