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
import os
import pandas as pd
base_dir = os.getcwd()
DATA_PATH = os.path.join(base_dir, "_data")
MODEL_PATH = os.path.join(base_dir, "_models")
def remove_file(path):
try:
os.remove(path)
except Exception as e:
print(e)
def save_df_to_csv(df, file):
print(df.head(3))
full_path = os.path.join(DATA_PATH, file)
print(full_path)
remove_file(full_path)
df.to_csv(full_path, sep="|", index=False)
def save_dict_to_csv(d, file):
print(len(d))
full_path = os.path.join(DATA_PATH, file)
print(full_path)
remove_file(full_path)
with open(full_path, "w") as f:
for (k, v) in d.items():
if v:
f.write("{}|{}\n".format(k, ",".join([str(x) for x in v])))
def get_df(file, sep="|", columns=[]):
full_path = os.path.join(DATA_PATH, file)
# full_path = os.path.join("/Users/offic/work/GM/strategy_embedding/_data", file) # TODO
print(full_path)
if columns:
df = pd.read_csv(full_path, sep=sep, names=columns)
else:
df = pd.read_csv(full_path, sep=sep)
print(df.shape)
return df