Files
data-management/general_function.py
Yam ZhengLim 4ff602f2c6 C2M Scrape add
2024-01-27 14:46:57 +08:00

46 lines
1.3 KiB
Python

import json
from operator import concat
import os
import pandas as pd
def read_json_file(path, name):
try:
with open(concat(path,name)) as json_file:
output = json.load(json_file)
return output
except Exception as e:
print(e)
print("[FUNCTION_ERROR]-read_json_file:" , name)
return False
def delete_csv_in_path(path):
for folder, subfolders, files in os.walk(path):
for file in files:
if file.endswith('.csv'):
path = os.path.join(folder, file)
os.unlink(path)
print("[Delete Temp Complete]", path)
def delete_csv_or_png_in_path(path):
for folder, subfolders, files in os.walk(path):
for file in files:
if file.endswith('.csv') or file.endswith('.png'):
path = os.path.join(folder, file)
os.unlink(path)
print("[Delete Temp Complete]", path)
def change_to_current_directory():
script_dir = os.path.dirname(os.path.realpath(__file__))
os.chdir(script_dir)
def json_normalize_to_df(json, sep='.'):
sep_char = sep
try:
df = pd.json_normalize(json, sep=sep_char)
return df
except Exception as e:
print(e)
print("[FUNCTION_ERROR]-json_normalize_to_df")
return False