spreadsheet: Read and write spreadsheets from and to CSV files in a lazy way

[ bsd3, data, library, text ] [ Propose Tags ]

Read and write spreadsheets from and to files containing comma separated values (CSV) in a lazy way. Reading from other source than plain Strings could be easily added.

If you install this package by

cabal install -fbuildExamples

then the example programs csvreplace and csvextract are compiled and installed, too. The program csvreplace fills a template text using data from a CSV file. For similar (non-Haskell) programs see cut, csvfix, csvtool. The program csvextract is the inverse of csvreplace.

Related packages:


[Skip to Readme]

Flags

Automatic Flags
NameDescriptionDefault
buildexamples

Build example executables

Disabled

Use -f <flag> to enable a flag, or -f -<flag> to disable that flag. More info

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

Versions [RSS] 0.1, 0.1.1, 0.1.1.1, 0.1.2, 0.1.2.1, 0.1.3, 0.1.3.1, 0.1.3.2, 0.1.3.3, 0.1.3.4, 0.1.3.5, 0.1.3.6, 0.1.3.7, 0.1.3.8, 0.1.3.9, 0.1.3.10
Dependencies base (>=2 && <5), containers (>=0.4.2 && <0.8), explicit-exception (>=0.1 && <0.3), optparse-applicative (>=0.12 && <0.19), shell-utility (>=0.0 && <0.2), spreadsheet, transformers (>=0.2 && <0.7), utility-ht (>=0.0.2 && <0.1) [details]
License BSD-3-Clause
Author Henning Thielemann <haskell@henning-thielemann.de>
Maintainer Henning Thielemann <haskell@henning-thielemann.de>
Category Data, Text
Home page http://www.haskell.org/haskellwiki/Spreadsheet
Source repo head: darcs get http://code.haskell.org/~thielema/spreadsheet/
this: darcs get http://code.haskell.org/~thielema/spreadsheet/ --tag 0.1.3.10
Uploaded by HenningThielemann at 2023-12-01T17:26:51Z
Distributions LTSHaskell:0.1.3.10, NixOS:0.1.3.10, Stackage:0.1.3.10
Reverse Dependencies 1 direct, 0 indirect [details]
Executables csvextract, csvreplace
Downloads 11550 total (50 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs uploaded by user
Build status unknown [no reports yet]

Readme for spreadsheet-0.1.3.10

[back to package description]

Example: csvreplace

If you build the package with the Cabal flag -fbuildExamples then the program csvreplace will be built. It allows you to replace placeholders in a template file according to the columns of a CSV file. E.g. given a file template.txt with content

Name: FIRSTNAME SURNAME
Born: BIRTH

and names.csv with content

"FIRSTNAME","SURNAME",BIRTH
"Georg","Cantor",1845
"Haskell","Curry",1900
"Ada","Lovelace",1815

the call

csvreplace template.txt <names.csv

produces the output

Name: Georg Cantor
Born: 1845
Name: Haskell Curry
Born: 1900
Name: Ada Lovelace
Born: 1815

You may also generate one file per CSV row in the following manner:

csvreplace --multifile=FIRSTNAME-SURNAME.txt template.txt <names.csv

Character Encoding

For simple replacement of parts of the text we would not need to decode the input texts and thus we would not need to know the used encoding scheme. Essentially, we would only require that both CSV and template file employ the same character encoding.

However, it is not as simple as that. We need to decode the structure of the CSV file. In multi-file mode we also need to generate proper file names. Both requirements force us to decode both CSV and template file. For the de- and encoding we use the default locale encoding.

If you want essentially a byte-by-byte replacement and you assert that all files are in the same encoding where the commas and quotation marks are compatible with ASCII then you can set the encoding locally to a complete 8-bit encoding like latin1 as in:

LANG=de_DE csvreplace --multifile=FIRSTNAME-SURNAME.txt template.txt <names.csv

Example: csvextract

This is somehow the inverse of csvreplace. Given a text file that was generated by substituting placeholders in a regular way. You can then obtain back a CSV file.

E.g. take the example files from csvreplace and call

csvreplace template.txt <names.csv | csvextract --columns FIRSTNAME,SURNAME,BIRTH template.txt

You should get back names.csv.

This is, how it works: The text in template.txt is first divided into text and placeholders according to the comma separated list of names for the --columns option. Then the program matches the template fragments with the input text and assigns the text between template fragments to the placeholders. Placeholder replacements are chosen as short as possible in a greedy way, i.e. per placeholder, not globally.

If you want to skip larger portions of the input text, you may use a placeholder like SKIP in template.txt and call csvextract with the option --ignore SKIP.