You can read CSV files in J pretty simply without using any predefined
verbs like this:
mat=. <.;_1&>',',&.><;._2 CR-.~freads jpath '~temp/test.csv'
But this does not properly handle cases where the delimiter is embedded in
a string, e.g. a file like this will cause problems:
1,2,"embed, comma",4.5
6,7,"no embedded comma",8.9
Also, I usually use the more general "dsv" (delimiter separated values)
module as I prefer tab-delimited files. You can look at these here:
load '~addons/tables/dsv/dsv.ijs'
mat=. (',';'"') readdsv jpath '~temp/test.csv'
NB. Read comma-delimited file with double-quotes around some items.
On Fri, Dec 6, 2013 at 10:25 AM, Jon Hough <[email protected]> wrote:
> I am a complete beginner to J. I am attempting to write and read CSV files.
>
> My initial attempt is to follow this method:
> http://www.jsoftware.com/jwiki/Addons/tables/csv
>
> I am using J-602 on 32-bit Linux.
> My attempt has some differences to the output shown in the example.
>
> dat writecsv jpath '~temp/test.csv'
> 45
>
> When I attempt this function it outputs 45, not 47 as in the example.
> (Not that I really know what either indicate at the moment).
>
> Then when I try to read the csv table, which does exist in my temps
> directory,
> I get this output:
>
> ]datcsv=: freads jpath '~temp/test.csv'
> 34,"45","hello",-5.34
> 12,"32","goodbye",1.23
>
> fixcsv datcsv
> |value error: chopstring
> | >msk <@(x&chopstring);._2 y
>
> And then I do the following:
>
> chopstring
> |value error: chopstring
>
> and:
>
> fixcsv
> ┌────────────┐
> │fixcsv_pcsv_│
> └────────────┘
>
> and:
>
> writecsv
> ┌──────────────┐
> │writecsv_pcsv_│
> └──────────────┘
>
> I am not sure what the final two outputs mean, but the
>
> |value error: chopstring
>
> appears to indicate that the function (verb?) chopstring does not exist.
>
> I am really interesting in using J to analyze datasets from CSV files, and
> although I as a complete beginner, I would appreciate any help.
>
> Regards,
> Jon
>
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
--
Devon McCormick, CFA
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm