mirror of
https://github.com/m1ngsama/tracker.git
synced 2025-12-24 10:51:43 +00:00
Add data export functionality (JSON and CSV)
This commit is contained in:
parent
cb57833561
commit
899cdbc4fa
2 changed files with 41 additions and 0 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
|
@ -25,3 +25,4 @@ venv/
|
|||
.DS_Store
|
||||
logs/
|
||||
*.log
|
||||
exports/
|
||||
|
|
|
|||
40
data_exporter.py
Normal file
40
data_exporter.py
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
"""
|
||||
Data export functionality
|
||||
"""
|
||||
|
||||
import json
|
||||
import csv
|
||||
from datetime import datetime
|
||||
|
||||
|
||||
class DataExporter:
|
||||
def __init__(self, output_dir='exports'):
|
||||
self.output_dir = output_dir
|
||||
|
||||
def export_to_json(self, data, filename=None):
|
||||
"""Export data to JSON format"""
|
||||
if filename is None:
|
||||
filename = f"tracker_data_{datetime.now().strftime('%Y%m%d_%H%M%S')}.json"
|
||||
|
||||
filepath = f"{self.output_dir}/{filename}"
|
||||
|
||||
with open(filepath, 'w') as f:
|
||||
json.dump(data, f, indent=2)
|
||||
|
||||
return filepath
|
||||
|
||||
def export_to_csv(self, data, filename=None):
|
||||
"""Export data to CSV format"""
|
||||
if filename is None:
|
||||
filename = f"tracker_data_{datetime.now().strftime('%Y%m%d_%H%M%S')}.csv"
|
||||
|
||||
filepath = f"{self.output_dir}/{filename}"
|
||||
|
||||
if isinstance(data, list) and len(data) > 0:
|
||||
keys = data[0].keys()
|
||||
with open(filepath, 'w', newline='') as f:
|
||||
writer = csv.DictWriter(f, fieldnames=keys)
|
||||
writer.writeheader()
|
||||
writer.writerows(data)
|
||||
|
||||
return filepath
|
||||
Loading…
Reference in a new issue