Skip to main content

A simple way of working with csv files.

Project description


A simple way of working with csv files.

You can use:

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

Instead of:

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

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

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

Also you can declaratively define types in a table's header:

>>> print(open("/tmp/csvfile_test.csv").read())

>>> pprint(csvfile.load("/tmp/csvfile_test.csv"))
[{'created': 1991, 'language': 'python'},
 {'created': 1995, 'language': 'js'},
 {'created': 2010, 'language': 'rust'}]

Notice, that created is automatically converted into integer as we typed it in the header as created:i.

Built-in types

Type Alias Comment
str s default, if no type specified
bool b reader can understand next pairs as True / False case-insensitevely: true / false, t / f, 1 / 0, y / n, yes / no, on / off, but the writer will always stringify them into true / false
int i
float f
decimal n
date d ISO 8601, YYYY-MM-DD
datetime t ISO 8601, YYYY-MM-DDTHH:MM:SS.ffffff
json j

Empty cells will become None for any type except of str. As in case of str there's no way to distinguish it from an empty string.

Running tests

pip install -r requirements-dev.txt

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

csvfile-3.0.0-py3-none-any.whl (3.8 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