Addons/tables/csv

From J Wiki
Jump to navigation Jump to search
User Guide | Installation | Development | Categories | Git | Build Log

tables/csv - CSV utilities

  • Provides verbs to read from and write to comma-separated-value (CSV) files or strings.
  • supports appending arrays to an existing csv file,
  • ability to convert fields to numeric type where possible
  • old code that uses the base library csv script should not need any modification

(apart from loading) to use this addon instead

  • CSV is a specific case of delimiter-separated-value (DSV) format and the verbs in this addon are covers of those in tables/dsv addon

Browse history, source and examples on GitHub.


Verbs available

appendcsv v Appends an array to a csv file
fixcsv v Convert csv data into J array
makecsv v Makes a CSV string from an array
makenum v Converts cells in array of boxed literals to numeric where possible
enclose v Encloses string in quotes
readcsv v Reads csv file into a boxed array
writecsv v Writes an array to a csv file

Installation

Use JAL/Package Manager to install both the tables/csv and tables/dsv addons.

If you wish to replace the use of the base library csv script with the tables/csv addon, add the following lines to your ~config/startup.ijs script:

PUBLIC_j_=: (<<<({."1 PUBLIC_j_) i. <'csv'){PUBLIC_j_
buildpublic_j_ 0 : 0
csv       ~addons/tables/csv/csv
)

If you do this, then require 'csv' and load 'csv' will target the csv addon rather than the base library csv script.

Usage

Load csv addon with the following line

   load 'tables/csv'

Verbs are documented in the csv.ijs] script.

   ]dat=: (34;'45';'hello';_5.34),: 12;'32';'goodbye';1.23
┌──┬──┬───────┬─────┐
│34│45│hello  │_5.34│
├──┼──┼───────┼─────┤
│12│32│goodbye│1.23 │
└──┴──┴───────┴─────┘
   datatype each dat
┌───────┬───────┬───────┬────────┐
│integer│literal│literal│floating│
├───────┼───────┼───────┼────────┤
│integer│literal│literal│floating│
└───────┴───────┴───────┴────────┘
   makecsv dat
34,"45","hello",-5.34
12,"32","goodbye",1.23

   dat writecsv jpath '~temp/test.csv'
47
   ]datcsv=: freads jpath '~temp/test.csv'
34,"45","hello",-5.34
12,"32","goodbye",1.23

   fixcsv datcsv
┌──┬──┬───────┬─────┐
│34│45│hello  │-5.34│
├──┼──┼───────┼─────┤
│12│32│goodbye│1.23 │
└──┴──┴───────┴─────┘
   readcsv jpath '~temp/test.csv'
┌──┬──┬───────┬─────┐
│34│45│hello  │-5.34│
├──┼──┼───────┼─────┤
│12│32│goodbye│1.23 │
└──┴──┴───────┴─────┘

Note that if you wish to use custom field and/or string delimiters, please see the tables/dsv addon (the tables/csv addon is a special case of the tables/dsv addon with the field delimiter set to ',' and the string delimiter set to '"'.

To see more samples of usage, open and inspect the test_csv.ijs script.

Authors

Adapted from the base library csv script by Ric Sherlock

Suggestions and/or improvements to the addon are welcome.

See Also

  • csvedit addon - GUI application for creating and editing CSV files.
  • dsv addon - general utility for any delimiter-separated-value formated string.