Spaces:
Sleeping
Sleeping
File size: 4,259 Bytes
f89e218 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
"""
Utility functions for data handling and export.
"""
import os
import json
import csv
import pandas as pd
from datetime import datetime
def save_json(data, file_path):
"""
Save data to a JSON file.
Args:
data: data to save
file_path: path to the output file
Returns:
bool: True if successful, False otherwise
"""
try:
with open(file_path, 'w', encoding='utf-8') as f:
json.dump(data, f, indent=2, ensure_ascii=False)
return True
except Exception as e:
print(f"Error saving JSON: {e}")
return False
def load_json(file_path):
"""
Load data from a JSON file.
Args:
file_path: path to the JSON file
Returns:
dict: loaded data, or None if an error occurred
"""
try:
with open(file_path, 'r', encoding='utf-8') as f:
return json.load(f)
except Exception as e:
print(f"Error loading JSON: {e}")
return None
def save_csv(data, file_path, headers=None):
"""
Save data to a CSV file.
Args:
data: list of dictionaries or list of lists
file_path: path to the output file
headers: optional list of column headers
Returns:
bool: True if successful, False otherwise
"""
try:
if isinstance(data, list) and len(data) > 0:
if isinstance(data[0], dict):
# List of dictionaries
if headers is None:
headers = list(data[0].keys())
with open(file_path, 'w', newline='', encoding='utf-8') as f:
writer = csv.DictWriter(f, fieldnames=headers)
writer.writeheader()
writer.writerows(data)
else:
# List of lists
with open(file_path, 'w', newline='', encoding='utf-8') as f:
writer = csv.writer(f)
if headers:
writer.writerow(headers)
writer.writerows(data)
return True
except Exception as e:
print(f"Error saving CSV: {e}")
return False
def dataframe_to_formats(df, base_path, formats=None):
"""
Export a pandas DataFrame to multiple formats.
Args:
df: pandas DataFrame
base_path: base path for output files (without extension)
formats: list of formats to export to ('csv', 'excel', 'html', 'json')
Returns:
dict: dictionary with format names as keys and file paths as values
"""
if formats is None:
formats = ['csv', 'excel', 'html']
result = {}
try:
for fmt in formats:
if fmt == 'csv':
file_path = f"{base_path}.csv"
df.to_csv(file_path)
result['csv'] = file_path
elif fmt == 'excel':
file_path = f"{base_path}.xlsx"
df.to_excel(file_path)
result['excel'] = file_path
elif fmt == 'html':
file_path = f"{base_path}.html"
df.to_html(file_path)
result['html'] = file_path
elif fmt == 'json':
file_path = f"{base_path}.json"
df.to_json(file_path, orient='records', indent=2)
result['json'] = file_path
except Exception as e:
print(f"Error exporting DataFrame: {e}")
return result
def generate_timestamp():
"""
Generate a timestamp string for file naming.
Returns:
str: timestamp string
"""
return datetime.now().strftime("%Y%m%d_%H%M%S")
def create_results_filename(prefix="evaluation", extension=""):
"""
Create a filename for results with timestamp.
Args:
prefix: prefix for the filename
extension: file extension (with or without dot)
Returns:
str: filename with timestamp
"""
timestamp = generate_timestamp()
if extension:
if not extension.startswith('.'):
extension = f".{extension}"
return f"{prefix}_{timestamp}{extension}"
else:
return f"{prefix}_{timestamp}"
|