Skip to main content

A simple way of working with csv files.

Project description

csvfile

A simple way of working with csv files.

You can use:

data = csvfile.load("my-data.csv")
data[0]["field"] = "new value"
data.sync()

Instead of:

data = []
with open("my-data.csv") as f:
    reader = csv.DictReader(f)
    fieldnames = reader.fieldnames
    for row in reader:
        data.append(row)

data[0]["field"] = "new value"

with open("my-data.csv, "w") as f:
    writer = csv.DictWriter(f, fieldnames=fieldnames)
    writer.writeheader()
    writer.writerows(data)

Typing

You also can provide a model based on pydantic.BaseModel. This will give you types checking and conversion:

>>> import csvfile, pydantic, decimal, datetime
>>> class Transaction(pydantic.BaseModel):
...     amount: decimal.Decimal
...     at: datetime.datetime
>>> table = csvfile.CSVFile("./transactions.csv", model=Transaction)
>>> table.append(Transaction(amount="10.5", at=datetime.datetime.now()))
>>> open("./transactions.csv").read()
'amount,at\n10.5,2020-03-11 14:13:31.087455\n'
>>> csvfile.load("./transactions.csv", model=Transaction)
[Transaction(amount=Decimal('10.5'), at=datetime.datetime(2020, 3, 11, 14, 13, 31, 87455))]

Running tests

pip install -r requirements-dev.txt
pytest

Project details


Download files

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

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distribution

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

csvfile-2.1.1-py3-none-any.whl (2.6 kB view details)

Uploaded Python 3

File details

Details for the file csvfile-2.1.1-py3-none-any.whl.

File metadata

  • Download URL: csvfile-2.1.1-py3-none-any.whl
  • Upload date:
  • Size: 2.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.23.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.43.0 CPython/3.8.1

File hashes

Hashes for csvfile-2.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 35982004778e07dc0ff9b6d2ae76beaf6906aac96f531afc9e3b339de33f2370
MD5 ca5a39296a8b28910eae41e05eacbfa3
BLAKE2b-256 dc148a5e74eb5a98ffd1ef49a7216b57dbc8770d25356d1f3c15f62fb1d1f64a

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