Loader Module

The Loader module provides robust file loading capabilities for various formats with automatic encoding detection.

class ABConnect.Loader.FileLoader(file_path, key=None, interactive=True)[source]

Bases: object

Loads data from CSV, JSON, or XLSX files while handling unsupported characters.

file_path

The path to the file.

Type:

str

key

Key to convert data into a dictionary form.

Type:

Optional[str]

data

The loaded data.

Type:

List[Dict[str, Any]]

__init__(file_path, key=None, interactive=True)[source]
load()[source]
Return type:

None

to_list()[source]

Returns the loaded data as a list of dictionaries.

Return type:

List[Dict[str, Any]]

to_dict()[source]

Converts the loaded data into a dictionary using self.key for keys.

Raises:

KeyError – If self.key is not found in a row.

Return type:

Dict[str, Any]

FileLoader

class ABConnect.Loader.FileLoader(file_path, key=None, interactive=True)[source]

Bases: object

Loads data from CSV, JSON, or XLSX files while handling unsupported characters.

file_path

The path to the file.

Type:

str

key

Key to convert data into a dictionary form.

Type:

Optional[str]

data

The loaded data.

Type:

List[Dict[str, Any]]

__init__(file_path, key=None, interactive=True)[source]
load()[source]
Return type:

None

to_list()[source]

Returns the loaded data as a list of dictionaries.

Return type:

List[Dict[str, Any]]

to_dict()[source]

Converts the loaded data into a dictionary using self.key for keys.

Raises:

KeyError – If self.key is not found in a row.

Return type:

Dict[str, Any]

Supported Formats

  • CSV - Comma-separated values files

  • JSON - JavaScript Object Notation files

  • XLSX/XLS - Excel spreadsheet files

Features

  • Automatic encoding detection for text files

  • Character encoding cleanup

  • Support for multiple Excel sheets

  • Robust error handling

Usage Examples

Loading CSV Files

from ABConnect.Loader import FileLoader

loader = FileLoader()

# Load CSV with automatic encoding detection
df = loader.load('data.csv')

# Load with specific encoding
df = loader.load('data.csv', encoding='utf-8')

Loading Excel Files

# Load specific sheet
df = loader.load('data.xlsx', sheet_name='Sheet1')

# Load all sheets
sheets = loader.load('data.xlsx', sheet_name=None)

# Access individual sheets
sheet1_df = sheets['Sheet1']

Loading JSON Files

# Load JSON data
data = loader.load('data.json')

# Works with nested structures
config = loader.load('config.json')
api_key = config['api']['key']

Error Handling

from ABConnect.exceptions import ABConnectError

try:
    data = loader.load('nonexistent.csv')
except ABConnectError as e:
    print(f"Failed to load file: {e}")

Character Encoding

The loader automatically handles various character encoding issues:

  • BOM (Byte Order Mark) removal

  • Invalid character replacement

  • Smart encoding detection using chardet