-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata_io.py
63 lines (46 loc) · 2.06 KB
/
data_io.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import json
import yaml
import pandas as pd
from typing import Any, Dict, List, Optional
OutputFile = Optional[str]
def write_dict_to_json_file(res_json: Any, output_filename: OutputFile = None) -> None:
if output_filename is not None:
with open(output_filename, 'w') as f:
json.dump(res_json, f)
def read_dict_from_json_file(json_file_path: str) -> Dict:
with open(json_file_path) as infile:
return json.load(infile)
def write_str_to_yaml_file(res_yaml: str, output_filename: OutputFile = None) -> None:
if output_filename is not None:
with open(output_filename, 'w') as outfile:
yaml.dump(res_yaml, outfile)
def read_dict_from_yaml_str(yaml_str: str) -> Optional[Dict[str, Any]]:
try:
yaml_dict = yaml.load(yaml_str, Loader=yaml.BaseLoader)
if type(yaml_dict) is dict:
return yaml_dict
except yaml.scanner.ScannerError as e:
print("ERROR: YAML parsing raised ScannerError.")
except yaml.parser.ParserError as e:
print("ERROR: YAML parsing raised ParserError.")
def write_df_to_csv_file(df: pd.DataFrame, output_filename: OutputFile = None,
header: Optional[bool] = False,
write_index_col: Optional[bool] = False) -> None:
if output_filename is not None:
df.to_csv(output_filename, header=header, index=write_index_col)
def read_df_from_csv_file(csv_file_path: str, column_names: Optional[List[str]] = None,
expect_index_col: Optional[bool] = False) -> pd.DataFrame:
return pd.read_csv(
csv_file_path,
index_col=expect_index_col,
names=column_names
)
def write_series_to_json_file(series: pd.Series, output_filename: OutputFile = None,
write_index_col: Optional[bool] = True) -> None:
if output_filename is not None:
series.to_json(output_filename, index=write_index_col)
def read_series_from_json_file(json_file_path: str) -> pd.Series:
return pd.read_json(
json_file_path,
typ='series'
)