Skip to main content

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)

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

>>> print(open("/tmp/csvfile_test.csv").read())
language,created:i
python,1991
js,1995
rust,2010

>>> 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.

API

The api consists in a single function load with the signature:

table = load(
    filename: Union[Path, str],   # the file should exist and contain headers
    *csvargs: Any,                # `*args` for the standard `csv.reader`
    limit: Optional[int] = None,  # maximum number of rows to read
    **csvkwargs: Any,             # `**fmtparams` for the standard `csv.reader`
)

The returned object has a list interface with an additional sync method to save changes back into the file.

Running tests

pip install -r requirements-dev.txt
pytest

Release files for csvfile 3.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Built distribution (wheel)

Table of built distributions (wheels) for csvfile 3.4.0
File Interpreter ABI Platform
csvfile-3.4.0-py3-none-any.whl Python 3 none any Details

Release files / csvfile-3.4.0-py3-none-any.whl

Download URL csvfile-3.4.0-py3-none-any.whl
Size 4.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0abc062fc43f00a3eba0082ca2744d066135e4232993e9f93adcc78e980e0006
BLAKE2b-256 checksum
How to use checksums
7c6b4800c3305e01ae1af9fa933293c1c96ecd51a294012099e6a99856d36d2e
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

3.4.0 This release

1 release file

3.3.0

1 release file

3.2.0

1 release file

3.1.0

1 release file

3.0.1

1 release file

3.0.0

1 release file

2.1.1

1 release file

2.1.0

1 release file

2.0.0

1 release file

1.0.0

1 release file

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page