-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils_data.py
48 lines (33 loc) · 1.04 KB
/
utils_data.py
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
import configparser
import json
import os
import pickle
import yaml
from yaml.loader import SafeLoader
def save_pickle(pickle_path, save_object):
with open(pickle_path, "wb") as handle:
pickle.dump(save_object, handle, protocol=pickle.HIGHEST_PROTOCOL)
def load_pickle(pickle_path):
with open(pickle_path, "rb") as handle:
return pickle.load(handle)
def load_yaml(yaml_path):
with open(yaml_path) as f:
data = yaml.load(f, Loader=SafeLoader)
return data
def load_txt(txt_path):
with open(txt_path) as f:
lines = f.readlines()
return lines
def load_ini(ini_path):
config = configparser.ConfigParser()
config.read(ini_path)
return config
def load_json(json_path):
if os.path.isfile(json_path):
with open(json_path, "r") as json_file:
return json.load(json_file)
else:
return None
def save_json(json_dict, output_path):
with open(output_path, "w", encoding="utf-8") as file:
json.dump(json_dict, file, ensure_ascii=False, indent=4)