Addons/tables/dsv

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

tables/dsv - Delimiter-Separated-Value utilities

  • Provides verbs to read from and write to delimiter-separated-value (DSV) files or strings,
  • supports appending arrays to an existing dsv file,
  • optional user-defined field delimiter (default is TAB)
  • optional user-defined string delimiter(s) (default is ")
  • ability to convert fields to numeric type where possible

The tables/dsv addon can be used for comma-separated-value files (CSV) files, but the tables/csv addon handles that format specifically.

Browse history, source and examples on GitHub.


Verbs available

appenddsv v Appends an array to a dsv file
enclose v Encloses string in quotes
fixdsv v Convert dsv data into J array
makedsv v Makes a DSV string from an array
makenum v Converts cells in array of boxed literals to numeric where possible
makenumcol v Converts columns of boxed literals to numeric if possible for all cells in a column
readdsv v Reads dsv file into a boxed array
writedsv v Writes an array to a dsv file

Installation

Use JAL/Package Manager.

Load dsv addon with the following line

   load 'tables/dsv'

Examples of use

    • only literal cells of J array are enclosed by string delimiters
   ]tstarry=: ('Hello';44.3;'to you';56),:'to me';39.22;'Bye now';'72'
┌─────┬─────┬───────┬──┐
│Hello│44.3 │to you │56│
├─────┼─────┼───────┼──┤
│to me│39.22│Bye now│72│
└─────┴─────┴───────┴──┘
   makedsv tstarry
"Hello"	44.3	"to you"	56
"to me"	39.22	"Bye now"	"72"

   (',';'{}') makedsv tstarry
{Hello},44.3,{to you},56
{to me},39.22,{Bye now},{72}

makenum and makenumcol can be used to convert cells that are string representations of numbers to numeric.

   ]tstarry=: makedsv 8!:0 i. 3 4
"0"	"1"	"2"	"3"
"4"	"5"	"6"	"7"
"8"	"9"	"10"	"11"

   fixdsv tstarry
┌─┬─┬──┬──┐
│0│1│2 │3 │
├─┼─┼──┼──┤
│4│5│6 │7 │
├─┼─┼──┼──┤
│8│9│10│11│
└─┴─┴──┴──┘
   1+ makenum fixdsv tstarry
1  2  3  4
5  6  7  8
9 10 11 12
 * writedsv and makedsv can handle boxed arrays with cells containing numeric

arrays, boxed or complex data

   ]tstarry=: ((34j3;2;<<4),:2;3 6;3)
┌────┬───┬───┐
│34j3│2  │┌─┐│
│    │   ││4││
│    │   │└─┘│
├────┼───┼───┤
│2   │3 6│3  │
└────┴───┴───┘

   load 'tables/dsv'
   ';' makedsv tstarry
34j3;2;4
2;3 6;3
 * fixdsv unescapes quotes embedded in fields
   tstdsv=: '"Symbol "" is Rank"|38|"abc"',LF,'"Hello world"|56|"efg"',LF
   ('|';'""') fixdsv tstdsv
┌────────────────┬──┬───┐
│Symbol " is Rank│38│abc│
├────────────────┼──┼───┤
│Hello world     │56│efg│
└────────────────┴──┴───┘

To see more examples of usage, open and inspect the test_dsv.ijs script.

Authors

Contributed by Ric Sherlock

Suggestions and/or improvements (e.g. GitHub pull request) to the addon are welcome.

See Also

  • csv addon - Utilities for working with comma-separated-value (CSV) files.
  • csvedit addon - GUI application for creating and editing CSV files.