Skip to content
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions pslab/utils/data_export.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import csv
import json

def export_to_csv(data, filename):
"""
data: list of dictionaries
"""
if not data:
return

with open(filename, "w", newline="") as f:
Comment thread
sourcery-ai[bot] marked this conversation as resolved.
Outdated
writer = csv.DictWriter(f, fieldnames=data[0].keys())
writer.writeheader()
writer.writerows(data)

Comment on lines +9 to +30
Copy link

Copilot AI Feb 8, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Both exporters raise on empty data, which prevents writing a valid empty JSON array/object and also prevents writing a header-only CSV when fieldnames is provided. Consider allowing empty iterables (and for CSV, requiring/providing fieldnames when data is empty) so exports can represent “no samples yet” cases.

Copilot uses AI. Check for mistakes.

def export_to_json(data, filename):
with open(filename, "w") as f:
json.dump(data, f, indent=4)