Skip to main content

Enhanced CSV reader and writer with automatic type inference.

Project description

philiprehberger-csv-kit

Tests PyPI version License

Enhanced CSV reader and writer with automatic type inference.

Install

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

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.
infer_types(rows) Cast string values to int, float, bool, or None where possible.

Development

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

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.1.0.tar.gz (4.1 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.1.0-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: philiprehberger_csv_kit-0.1.0.tar.gz
  • Upload date:
  • Size: 4.1 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.1.0.tar.gz
Algorithm Hash digest
SHA256 6268efaae953ae91fbd35c89ef3c3dd10098825aa9d430fdd3ebcabc4e794857
MD5 e80ab1c3938ca58b2ea0b5dd3929350d
BLAKE2b-256 65d4144acc33b36bb7085dc1e14ef964fb9de0414452e83befcff9453f02c641

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for philiprehberger_csv_kit-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7f44a4a21682b17aaec95b21338ef67968778ff744f317f71f5b20613e612d78
MD5 0937fd181aed9d6f0f518591554ead26
BLAKE2b-256 176a4724debdc0cab326e5bba516dbb7a51fd7e68a6929cac4b03c9cdbc7e64b

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