File size: 1,584 Bytes
3db9763
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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 pickle  
import pandas as pd

def load_service_data(file_path: str) -> pd.DataFrame:
    """
    Loads a CSV file into a Pandas DataFrame and sets the index to the 'service' column.
    Parameters:
        file_path (str): Path to the CSV file.
    Returns:
        pd.DataFrame: DataFrame with 'service' as the index.
    """
    try:
        df = pd.read_csv(file_path)
        df = df.set_index("service")  # Set 'service' as index
        return df
    except FileNotFoundError:
        print(f"Error: The file '{file_path}' was not found.")
        return pd.DataFrame()  # Return an empty DataFrame on error
    except Exception as e:
        print(f"Error loading CSV file '{file_path}': {e}")
        return pd.DataFrame()
        
def load_pickle(file_path: str):
    """Loads and returns data from a Pickle (.pkl) file."""
    try:
        with open(file_path, "rb") as file:  # Open in 'rb' (read binary) mode
            return pickle.load(file)
    except FileNotFoundError:
        print(f"Error: The file '{file_path}' was not found.")
        return None
    except Exception as e:
        print(f"Error reading Pickle file '{file_path}': {e}")
        return None

def load_file(file_path: str) -> str:
    """Reads the text from a file safely."""
    try:
        with open(file_path, "r", encoding="utf-8") as file:
            return file.read()
    except FileNotFoundError:
        print(f"Error: The file '{file_path}' was not found.")
        return ""
    except Exception as e:
        print(f"Error reading file '{file_path}': {e}")
        return ""