Pyexcel IO

0.6.7 · active · verified Fri Apr 10

Pyexcel-io is a Python library providing a unified API to read and write structured data from various file formats (CSV, TSV, and extensions like XLS, XLSX, ODS via plugins) and databases (SQLAlchemy, Django models). It focuses on data processing, not formatting, fonts, or charts. It is actively maintained with frequent minor releases.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use `pyexcel-io` to write tabular data to a virtual CSV file (using `StringIO`) and then read it back. The core functions `save_data` and `get_data` provide a unified interface for various data sources and formats.

import os
from pyexcel_io import save_data, get_data
from io import StringIO

# --- Writing data to a virtual CSV file (StringIO) ---
data_to_write = [
    ["Name", "Age", "City"],
    ["Alice", 30, "New York"],
    ["Bob", 24, "London"]
]

output_stream = StringIO()
save_data(output_stream, data_to_write, file_type='csv')

# Simulate getting content from the stream
output_stream.seek(0)
csv_content = output_stream.getvalue()
print("Generated CSV content:\n" + csv_content)

# --- Reading data from the virtual CSV file (StringIO) ---
input_stream = StringIO(csv_content)
# get_data returns a dictionary where keys are sheet names (or 'csv' for single file)
dread_data = get_data(input_stream, file_type='csv')

# Access the first (and only) sheet, usually named 'csv'
if 'csv' in dread_data:
    print("\nRead data:")
    for row in dread_data['csv']:
        print(row)
else:
    print("Error: Could not read data from CSV stream.")

# Example with a physical file (uncomment to test locally)
# file_name = "example.csv"
# save_data(file_name, data_to_write)
# data_from_file = get_data(file_name)
# print(f"\nData from {file_name}: {data_from_file[file_name]}")
# os.remove(file_name) # Clean up

view raw JSON →