-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_save.py
More file actions
74 lines (54 loc) · 1.84 KB
/
load_save.py
File metadata and controls
74 lines (54 loc) · 1.84 KB
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
import os
import json
import pandas
def load(data_file):
"""Load the given data file and returns a dictionary of the values
Args:
data_file (str): Path to a file containing data in one of the known formats (json, csv)
Returns:
A dictionary with the loaded data
"""
_, ext = os.path.splitext(data_file)
try:
return known_file_types[ext]['load'](data_file)
except KeyError:
raise Exception('Error loading file: type ' + str(ext) + ' is not supported')
def save(data_file, dictionary):
"""Save the given dictionary in the given file. Format is determined by data_file extension
Args:
data_file (str): Path to a file in which to save the data. Extension is used to determine
the format, therefore the path must contain an extension.
dictionary (dict): Dictionary with the data
"""
_, ext = os.path.splitext(data_file)
try:
known_file_types[ext]['save'](data_file, dictionary)
except KeyError:
raise Exception('Error loading file: type ' + str(ext) + ' is not supported')
def _load_json(data_file):
with open(data_file, 'r') as file:
return json.load(file)
def _save_json(data_file, dictionary):
with open(data_file, 'w') as file:
json.dump(dictionary, file)
def _load_csv(data_file):
data_frame = pandas.read_csv(data_file, header=0)
res = {}
for k in data_frame:
res[k] = data_frame[k].tolist()
return res
def _save_csv(data_file, dictionary):
data_frame = pandas.DataFrame.from_dict(data=dictionary, orient='columns')
data_frame.to_csv(data_file, header=True, index=False)
known_file_types = {
'.json':
{
'load': _load_json,
'save': _save_json
},
'.csv':
{
'load': _load_csv,
'save': _save_csv
}
}