MathGroup Archive 1999

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: ReadList

  • To: mathgroup at smc.vnet.net
  • Subject: [mg18846] Re: ReadList
  • From: paulh at wolfram.com (P.J. Hinton)
  • Date: Thu, 22 Jul 1999 22:57:46 -0400
  • Organization: Wolfram Research, Inc.
  • References: <7n673v$gqg@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

In article <7n673v$gqg at smc.vnet.net>, "James l. Fisher" <jlfisher at imt.net> writes:

> I want to read the following data which is in ascii form into a list 
> 
> 09/22/98 12:15 2.36
> 09/22/98 12:30 2.37
> 09/22/98 12:45 2.38
> etc 
> 
>  How can I use ReadList, or some other command, to enter this into the form
> 
>  {{09/22/98,12:15,2.36},{09/22/98,12:30,2.37},{09/22/98,12:45, 2.38}}

The fastest way is to tell ReadList[] what types of data to expect.

In[1]:= ReadList["datafile.txt", {Word, Word, Number}]

Out[1]= {{09/22/98, 12:15, 2.36}, {09/22/98, 12:30, 2.37}, 
 
>    {09/22/98, 12:45, 2.38}}
 
> How could I read each column into a seperate list also.

Transpose the resulting list.

In[2]:= Transpose[%]

Out[2]= {{09/22/98, 09/22/98, 09/22/98}, {12:15, 12:30, 12:45}, 
 
>    {2.36, 2.37, 2.38}}

In Mathematica 4, you can use Import[] to read in the file and let
the the kernel try to parse things automatically.

In[3]:= Import["datafile.txt", "Table", 
          ConversionOptions -> {
            TableSeparators -> {{"\n"}, {"\t", " "}}
          }
        ]

Out[3]= {{09/22/98, 12:15, 2.36}, {09/22/98, 12:30, 2.37}, 
 
>    {09/22/98, 12:45, 2.38}}

Note that the date is returned as a string rather than a list.  
By default, two-digit years are not converted (this keeps us
in line with our Y2K compliance statement on our Technical
Support website).  You need to use the "TwoDigitYearFunction" 
suboption of ConversionOptions to provide an interpretation 
mechanism.

--
P.J. Hinton	
Mathematica Programming Group		paulh at wolfram.com
Wolfram Research, Inc.
Disclaimer: Opinions expressed herein are those of the author alone.


  • Prev by Date: how to calculate surfuce and content of ellipse shapes?
  • Next by Date: Re: Dialog boxes
  • Previous by thread: Re: ReadList
  • Next by thread: Re: ReadList