-
Notifications
You must be signed in to change notification settings - Fork 0
/
support.py
109 lines (88 loc) · 3.58 KB
/
support.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
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
import json
import pathlib
import pandas as pd
from pathlib import Path
# function to load Json data in a dataframe to a file
def load_json_to_file(json_data, file_path, mode=None):
try:
if mode != None:
with open(file_path, mode) as file:
json.dump(json_data, file, indent=4)
else:
with open(file_path, 'w') as file:
json.dump(json_data, file, indent=4)
except Exception as error:
print('Error in file creation: ' + repr(error))
# function to create a folder structure if it doesn't already exist
def create_folder(path):
try:
pathlib.Path(path).mkdir(parents=True, exist_ok=True)
except Exception as error:
print(repr(error))
# Function to get value based on column match
def get_value_by_column(df, column_name, match_value, return_column):
# Filter the dataframe
filtered_df = df.loc[df[column_name] == match_value]
# Check if the filtered dataframe is not empty
if not filtered_df.empty:
# Return the value from the specified return column
return filtered_df[return_column].values[0]
else:
return None
# function to load dataframe to parquet using gzip compression
def load_parquet(dataframe, file_name):
try:
dataframe.to_parquet(file_name, compression='gzip')
except Exception as error:
print(repr(error))
# function to load dataframe to csv using desired seperator
def load_csv(dataframe, file_name, seperator):
try:
dataframe.to_csv(file_name, index=False, sep=seperator)
except Exception as error:
print(repr(error))
# function to load data frame to excel file, can specify whether to append a new sheet to an existing file, or create a new document for the file
def load_excel(dataframe, file_name, sheet_name, single_file):
try:
# if not unified file, write to a new file with the specified file name
if single_file == False:
with pd.ExcelWriter(
file_name + '.xlsx',
mode='w',
engine='openpyxl'
) as writer:
dataframe.to_excel(writer, sheet_name=sheet_name, index=False)
else:
# if the file already exists, append a new sheet to the file (overwriting if the sheet already exists)
if Path(file_name + '.xlsx').is_file():
with pd.ExcelWriter(
file_name + '.xlsx',
mode='a',
engine='openpyxl',
if_sheet_exists='replace',
) as writer:
dataframe.to_excel(writer, sheet_name=sheet_name, index=False)
# if the file doesn't already exist, create it and write the data to a named sheet
else:
with pd.ExcelWriter(
file_name + '.xlsx',
mode='w',
engine='openpyxl'
) as writer:
dataframe.to_excel(writer, sheet_name=sheet_name, index=False)
except Exception as error:
print(repr(error))
def write_metadata_json(filename, type, new_data):
# open file
with open(filename, 'r+') as file:
file_data = json.load(file)
try:
# append to array
file_data[0][type].append(new_data)
except:
file_data[0][type] = []
file_data[0][type].append(new_data)
# set the file's current position at offset
file.seek(0)
# convert file back to json
json.dump(file_data, file, indent=4)