Skip to main content

Enhanced CSV reader and writer with automatic type inference.

Project description

philiprehberger-csv-kit

Tests PyPI version GitHub release Last updated License Bug Reports Feature Requests Sponsor

Enhanced CSV reader and writer with automatic type inference.

Installation

pip install philiprehberger-csv-kit

Usage

Reading CSV

from philiprehberger_csv_kit import read_csv

rows = read_csv("data.csv")
# [{"name": "Alice", "age": 30, "score": 9.5}, ...]

Values are automatically cast to int, float, bool, or None. Disable with typed=False:

rows = read_csv("data.csv", typed=False)
# [{"name": "Alice", "age": "30", "score": "9.5"}, ...]

Writing CSV

from philiprehberger_csv_kit import write_csv

rows = [
    {"name": "Alice", "age": 30, "score": 9.5},
    {"name": "Bob", "age": 25, "score": 8.0},
]

write_csv("output.csv", rows)
write_csv("output.csv", rows, columns=["name", "age"])  # select columns

Streaming large files

from philiprehberger_csv_kit import stream_csv

for chunk in stream_csv("large.csv", chunk_size=500):
    for row in chunk:
        process(row)

Column statistics

from philiprehberger_csv_kit import column_stats

stats = column_stats("data.csv")
# {"age": {"min": 25, "max": 30, "unique": 2, "nulls": 0, "count": 2}, ...}

# Analyse specific columns only
stats = column_stats("data.csv", columns=["age", "score"])

Type Inference

from philiprehberger_csv_kit import infer_types

raw = [{"val": "42"}, {"val": "3.14"}, {"val": "true"}, {"val": ""}]
typed = infer_types(raw)
# [{"val": 42}, {"val": 3.14}, {"val": True}, {"val": None}]

API

Function Description
read_csv(path, typed=True, encoding="utf-8") Read CSV file, return list of dicts. Infers types when typed=True.
write_csv(path, rows, columns=None, encoding="utf-8") Write list of dicts to CSV. Optional column filter.
stream_csv(path, chunk_size=1000, encoding="utf-8") Generator yielding chunks of row dicts for memory-efficient reading.
column_stats(path, columns=None) Compute per-column stats: min, max, unique, nulls, count.
infer_types(rows) Cast string values to int, float, bool, or None where possible.

Development

pip install -e .
python -m pytest tests/ -v

Support

If you find this package useful, consider starring the repository.

LinkedIn More Packages

License

MIT

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

philiprehberger_csv_kit-0.2.0.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

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

philiprehberger_csv_kit-0.2.0-py3-none-any.whl (5.4 kB view details)

Uploaded Python 3

File details

Details for the file philiprehberger_csv_kit-0.2.0.tar.gz.

File metadata

  • Download URL: philiprehberger_csv_kit-0.2.0.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.13

File hashes

Hashes for philiprehberger_csv_kit-0.2.0.tar.gz
Algorithm Hash digest
SHA256 72d3ffa8e45790e3b1df2c38f5979dc2d53379a3f94ef963e506f2404b916466
MD5 10f42c17150d592a612615303a839661
BLAKE2b-256 7f3ead89aa48abb23402812514b228e5a0c3af340dc4d75adac88f6a21bcf839

See more details on using hashes here.

File details

Details for the file philiprehberger_csv_kit-0.2.0-py3-none-any.whl.

File metadata

File hashes

Hashes for philiprehberger_csv_kit-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 a6235edcb847b23b943c3367f4c3a759a870b39a7ef7f927aea3ec93019c2876
MD5 e9da5403b692df3beb76168a725d8280
BLAKE2b-256 2023d579e93a5eada5fc6139e20b7edb30b16f29f992f37a070138b01c7a861c

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