Skip to main content

ElasticDictWriter module based on Python csv package ensuring consistent header in the final CSV.

Project description

CSV ElasticDictWriter

DictWriter, built on top of csv.DictWriter, that supports automatic extension of headers according to what data it receives. The result file always has a complete header (as defined by fieldnames) or it is extended if some new columns are introduced in the data. It always produces a valid CSV (missing columns are filled with blanks). It uses a series of cached writers / files that are merged into a single one with final set of columns on close()

Installation

The package may be installed via PIP:

pip install keboola.csvwriter

NOTE: If not using "with" statement, close() method must be called at the end of processing to get the result.

NOTE: Does not keep the order of rows added - the rows containing additional headers always come first:

Example:

from keboola.csvwriter import ElasticDictWriter
file = '/test/test.csv'
wr = ElasticDictWriter(file, ["a", "b" , "c"])
wr.writeheader()
wr.writerow({"a":1,"b":2})
wr.writerow({"b":2, "d":4})
wr.close()

leads to CSV with following content:

a b c d
2 4
1 2

May be also used with with statement to automatically close once finished:

from keboola.csvwriter import ElasticDictWriter
file = '/test/test.csv'
with ElasticDictWriter(file, ["a", "b" , "c"]) as wr:
    wr.writeheader()
    wr.writerow({"a":1,"b":2})
    wr.writerow({"b":2, "d":4})

# get final headers
final_header = wr.fieldnames

NOTE: The final column list is stored in fieldnames property:

columns = writer.fieldnames

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

keboola.csvwriter-1.0.1.tar.gz (4.2 kB view details)

Uploaded Source

Built Distribution

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

keboola.csvwriter-1.0.1-py3-none-any.whl (5.7 kB view details)

Uploaded Python 3

File details

Details for the file keboola.csvwriter-1.0.1.tar.gz.

File metadata

  • Download URL: keboola.csvwriter-1.0.1.tar.gz
  • Upload date:
  • Size: 4.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.2

File hashes

Hashes for keboola.csvwriter-1.0.1.tar.gz
Algorithm Hash digest
SHA256 2739dbe2808aa8e5e07f09a0183ba72e70af8125bc544ea85680e95331e6b945
MD5 f20d4038d0612554adffa1731a99a270
BLAKE2b-256 64b7ce1a3530856be8a14a3ee70d2a46393d489fe61daefe9cd14eed9503b17b

See more details on using hashes here.

File details

Details for the file keboola.csvwriter-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: keboola.csvwriter-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 5.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.7.1 importlib_metadata/4.10.1 pkginfo/1.8.2 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.7.2

File hashes

Hashes for keboola.csvwriter-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e20fba3bdfe15a9a08e82f25acc2273fa6632377b5958df8be57a8bbbb57ce34
MD5 acfd12a7d2d0efae5a27a15c61823cc3
BLAKE2b-256 31b743acb5e6dfd1968842df1cef38ae66fde09af1e37b83b9a7e56edd37528b

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