Skip to main content

A simple Python package for loading data from CSV and XLSX files

Project description

DataLoader

A Python class for loading data from CSV and XLSX files, with support for single files or concatenating multiple files from folders.

Features

  • Single File Loading: Read CSV or XLSX files individually
  • Folder Loading: Automatically concatenate all CSV/XLSX files in a folder
  • Subfolder Support: Option to include files from subfolders
  • Verbose Output: Control the level of detail in console output
  • Error Handling: Graceful handling of file errors and unsupported formats
  • Flexible Usage: Both class-based and function-based interfaces

Installation

  1. Install the required dependencies:
pip install -r requirements.txt
  1. Or install dependencies manually:
pip install pandas openpyxl xlrd

Dependencies

  • pandas (>=1.3.0): For data manipulation and DataFrame operations
  • openpyxl (>=3.0.0): For reading Excel (.xlsx) files
  • xlrd (>=2.0.0): For reading legacy Excel (.xls) files

Quick Start

Basic Usage

from data_loader import DataLoader

# Load a single file
loader = DataLoader("data.csv")
df = loader.load()

# Load all files from a folder
loader = DataLoader("data_folder")
df = loader.load()

Using the Convenience Function

from data_loader import load_data

# Direct loading
df = load_data("data.csv")
df = load_data("data_folder")

Detailed Usage

Class Initialization

DataLoader(file_path, include_subfolders=False, verbose=True, column_consistency='error')

Parameters:

  • file_path (str): Path to a file or folder
  • include_subfolders (bool): Whether to include files from subfolders (default: False)
  • verbose (bool): Whether to print detailed information (default: True)
  • column_consistency (str): How to handle column consistency ('error', 'warning', 'ignore') (default: 'error')

Examples

1. Single File Loading

from data_loader import DataLoader

# Load a CSV file
loader = DataLoader("sales_data.csv")
df = loader.load()

# Load an Excel file
loader = DataLoader("financial_report.xlsx")
df = loader.load()

Output:

sales_data.csv is imported with 1000 rows and 5 columns

2. Folder Loading (No Subfolders)

loader = DataLoader("data_folder", include_subfolders=False)
df = loader.load()

Output:

Found 3 files to process
data_1.csv is imported with 500 rows and 4 columns
data_2.csv is imported with 300 rows and 4 columns
data_3.xlsx is imported with 200 rows and 4 columns

Summary:
Successfully loaded 3 files
Combined dataset has 1000 rows and 4 columns

3. Folder Loading (With Subfolders)

loader = DataLoader("data_folder", include_subfolders=True)
df = loader.load()

This will recursively search through all subfolders and load all CSV/XLSX files.

4. Quiet Mode

loader = DataLoader("data.csv", verbose=False)
df = loader.load()

No console output will be displayed.

5. Column Consistency Control

# Error mode (default) - stops if columns don't match
loader = DataLoader("data_folder", column_consistency='error')
df = loader.load()

# Warning mode - shows warning but continues
loader = DataLoader("data_folder", column_consistency='warning')
df = loader.load()

# Ignore mode - skips consistency check entirely
loader = DataLoader("data_folder", column_consistency='ignore')
df = loader.load()

Column Consistency Modes:

  • 'error' (default): Raises an error if files have different column counts or names
  • 'warning': Shows a warning but continues processing
  • 'ignore': Skips consistency check entirely

6. Convenience Function

from data_loader import load_data

# All parameters are optional
df = load_data("data.csv")  # Uses defaults
df = load_data("data_folder", include_subfolders=True, verbose=False, column_consistency='warning')

Supported File Formats

  • CSV files: .csv
  • Excel files: .xlsx, .xls

Error Handling

The DataLoader handles various error scenarios:

  • File not found: Raises FileNotFoundError
  • Unsupported format: Raises ValueError with format information
  • Invalid path: Raises ValueError if path is neither file nor directory
  • Column consistency errors: Raises ValueError when column_consistency='error' and files have mismatched columns
  • Individual file errors: Continues processing other files and reports errors in verbose mode

Example Project Structure

project/
├── data_loader.py
├── requirements.txt
├── example_usage.py
├── README.md
├── data/
│   ├── sales_2023.csv
│   ├── sales_2024.csv
│   └── reports/
│       ├── monthly_report.xlsx
│       └── quarterly_summary.csv
└── single_file.csv

Running Examples

To see the DataLoader in action, run the example script:

python example_usage.py

This will create sample data files and demonstrate various usage patterns.

API Reference

DataLoader Class

Methods

  • load(): Load data from the specified path
    • Returns: pandas.DataFrame

Internal Methods

  • _load_single_file(): Load data from a single file
  • _load_folder(): Load and concatenate data from folder
  • _load_single_file_from_path(): Internal method for loading individual files

Convenience Function

  • load_data(file_path, include_subfolders=False, verbose=True, column_consistency='error'): Direct data loading function

Performance Notes

  • Large files are loaded into memory entirely
  • For very large datasets, consider processing files individually
  • Concatenation happens in memory, so ensure sufficient RAM for large folder operations

License

This project is open source and available under the MIT License.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

simple_data_loader-1.0.2.tar.gz (12.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

simple_data_loader-1.0.2-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file simple_data_loader-1.0.2.tar.gz.

File metadata

  • Download URL: simple_data_loader-1.0.2.tar.gz
  • Upload date:
  • Size: 12.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.2

File hashes

Hashes for simple_data_loader-1.0.2.tar.gz
Algorithm Hash digest
SHA256 f3bdacfa6586e9bfb3c1227c96731b20a6448997d4b3490661f2f90a8690aef2
MD5 095ea98d08bf1a7b2c17d5289239c231
BLAKE2b-256 069706a85c86ebc70c7de19174f10e3bc3603e88cca0364055dada177ef007ef

See more details on using hashes here.

File details

Details for the file simple_data_loader-1.0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for simple_data_loader-1.0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 f9a1d308080ef4d4e526f7e59c31c4ae901ff7d30d98d93939a77ff95f6f3f94
MD5 5302e9035f36831311b3b303a794d549
BLAKE2b-256 070e0f2e7dedcd155bd9929dd24b9ea233f7a1df17e4984be556ee5cb971ac49

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page