We did a "Beginner's Regatta" at NYCJUG earlier this year in which we
explored importing tabular data into J from delimited files:
http://www.jsoftware.com/jwiki/NYCJUG/2012-05-08#Beginner.27s_regatta with
a follow-up the following month:
http://www.jsoftware.com/jwiki/NYCJUG/2012-06-12#Follow-up_to_Explanation_of_.2BIBw-Box_Cut.2BIB0-.


On Fri, Nov 9, 2012 at 12:01 AM, Ric Sherlock <[email protected]> wrote:

> Purely a convenience thing, but using the verb freads rather than fread
> will take care of the line-ending issue, by converting to the J standard of
> LF-delimited lines. The verb fwrites will convert J line-endings to the
> conventional line-ending for your current platform.
> On Nov 8, 2012 12:34 PM, "Raul Miller" <[email protected]> wrote:
>
> > I am not sure what your line ends look like.
> >
> > A general implementation might be:
> >
> >    <;._2@,&TAB;._2 -.&CR ,&LF fread 'mytestfile.txt'
> >
> > This will have an extra blank row at the bottom if your file is
> > newline terminated, in which case you might want:
> >
> >    <;._2@,&TAB;._2 -.&CR fread 'mytestfile.txt'
> >
> > If you are on a unix system with no carriage returns (or if carriage
> > returns represent information for you) you might want to remove the
> > -.&CR
> >
> > (Also you could remove blank rows from the bottom of the resulting
> > data structure, if they are sometimes present and sometimes not.)
> >
> > Beware that some mail systems will mutilate ascii character 64, which
> > may render the above lines of code invalid.  So, here's a repackaged
> > statement of the above two lines of code:
> >
> > smoutput a.{~, ".0 :0-.LF
> >   32  32  32  60  59  46  95  50  64  44  38  84  65  66  59  46  95
> >   50  32  45  46  38  67  82  32  44  38  76  70  32 102 114 101  97
> >  100  32  39 109 121 116 101 115 116 102 105 108 101  46 116 120 116
> >   39  10  10  32  32  32  60  59  46  95  50  64  44  38  84  65  66
> >   59  46  95  50  32  45  46  38  67  82  32 102 114 101  97 100  32
> >   39 109 121 116 101 115 116 102 105 108 101  46 116 120 116  39  10
> > )
> >
> > I hope this helps.
> >
> > Thanks,
> >
> > --
> > Raul
> >
> > On Wed, Nov 7, 2012 at 6:00 PM, kamakura <[email protected]>
> > wrote:
> > > Hi
> > >
> > > I would like to know J's manipulation for reading text file.
> > >
> > > fd=:'mytestfile.txt' NB. TAB text file
> > > NB. a \t 1 \t 2 \t 3
> > > NB. b \t \t 5 \t 6
> > > NB. c \t 7 \t \t 9
> > > NB. d \t 10 \t 11 \t 12
> > > NB. \t 13 \t 14 \t 15
> > >
> > > fd2=:'mytestfile.csv' NB. CSV test file
> > >
> > > load'files misc'
> > >    freads fd2
> > > a,1,2,3
> > > b,,5,6
> > > c,7,,9
> > > d,10,11,12
> > > ,13,14,15
> > >
> > >
> > >    freadr fd ;0 1
> > > a       1       2       3
> > >
> > >    freadr fd ;1 1
> > > b               5       6
> > >
> > >    freadr fd ;2 1
> > >         7               9
> > > d
> > >    freadr fd ;3 1
> > > 10      11      1
> > >    freadr fd ;4 1
> > >
> > >         13      14
> > >
> > > The 3rd and 4th rows are read differently.  I expect that the following
> > line will come out.
> > >
> > >    freadr fd ;2 1
> > > c       7               9
> > >
> > >
> > >
> > >    u=:'m' fread fd
> > >    u
> > > a       1       2       3
> > > b               5       6
> > > c       7               9
> > > d       10      11      12
> > >         13      14      15
> > >
> > >    TAB chop "1 u
> > > +--+--+-----+----+
> > > |a |1 |2    |3   |
> > > +--+--+-----+----+
> > > |b |5 |6    |    |
> > > +--+--+-----+----+
> > > |c |7 |     |9   |
> > > +--+--+-----+----+
> > > |d |10|11   |12  |
> > > +--+--+-----+----+
> > > |13|14|15   |    |
> > > +--+--+-----+----+
> > >
> > >
> > > How can I get the following table?
> > >
> > > +--+--+-----+----+
> > > |a |1 |2    |3   |
> > > +--+--+-----+----+
> > > |b |NA|5    |6   |
> > > +--+--+-----+----+
> > > |c |7 |9    |NA  |
> > > +--+--+-----+----+
> > > |d |10|11   |12  |
> > > +--+--+-----+----+
> > > |NA|13|14   |15  |
> > > +--+--+-----+----+
> > >
> > > R reads this text file as follows:
> > >
> > >> u=read.table("mytestfile.txt",header=F,na.strings="",sep="\t")
> > >> u
> > >     V1 V2 V3 V4
> > > 1    a  1  2  3
> > > 2    b NA  5  6
> > > 3    c  7 NA  9
> > > 4    d 10 11 12
> > > 5 <NA> 13 14 15
> > >
> > > Do you have any convenient utility function for reading text files?
> > >
> > >
> > >
> > > +++++++++++++++++++++++++++++
> > >     Toshinari Kamakura
> > >
> > >     Chuo University
> > >     1-13-27 Kasuga
> > >     Bunkyo-ku
> > >     Tokyo 112-8551, Japan
> > > ++++++++++++++++++++++++++++++
> > >
> > >
> > >
> > > ----------------------------------------------------------------------
> > > For information about J forums see http://www.jsoftware.com/forums.htm
> > ----------------------------------------------------------------------
> > For information about J forums see http://www.jsoftware.com/forums.htm
> >
> ----------------------------------------------------------------------
> For information about J forums see http://www.jsoftware.com/forums.htm
>



-- 
Devon McCormick, CFA
^me^ at acm.
org is my
preferred e-mail
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to