Skip to main content

Write SQLAlchemy results to csv files

Project description

SQLAlchemyCsvWriter

Stable Version Python Versions Tests codecov Ruff Poetry

SQLAlchemyCsvWriter is a thin wrapper around the python csw.writer function to make the process of exporting SQLAlchemy query results to csv simpler.

It supports both synchronous as well as asynchronous query results, as well as streaming.

Features

  • Write SQLAlchemy query results to csv file with little boilerplate
  • Optionally write header by auto generation or passing column names
  • Supports same dialect and formatting parameters as csv.writer
  • Supports synchronous, asynchronous and asynchronous streaming results
  • Supports any models defined with DeclarativeBase

Installation

  • with pip: pip install sqlalchemy-csv-writer
  • with poetry: poetry add sqlalchemy-csv-writer

Usage

from sqlalchemy_csv_writer import SQLAlchemyCsvWriter

with SQLAlchemyCsvWriter(
    "test.csv",
    header=True,
    field_formats={"column_1": "%.2f"},
    prefix_model_names=True,
    dialect="unix",
) as writer:
    writer.write_rows(results)  # pass results of SQLAlchemy query

For full example see examples directory (uses the alchemical SQLAlchemy wrapper).

Development Notes

  • Linting and formatting using ruff
  • Testing using pytest
  • Dependency management and release using poetry
  • Documentation using mkdocs with mkdocs-material and other plugins, released to GitHub pages
  • CI/CD using GitHub Actions
  • Pull requests are welcome

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

sqlalchemy_csv_writer-0.4.2.tar.gz (4.5 kB view hashes)

Uploaded Source

Built Distribution

sqlalchemy_csv_writer-0.4.2-py3-none-any.whl (4.9 kB view hashes)

Uploaded Python 3

Supported by

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