MathGroup Archive 1996

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

Search the Archive

Re: TRYING to read ASCII data with Mathematica

  • To: mathgroup at smc.vnet.net
  • Subject: [mg3087] Re: [mg3079] TRYING to read ASCII data with Mathematica
  • From: John Fultz <jfultz>
  • Date: Wed, 31 Jan 1996 03:03:10 -0500
  • Sender: owner-wri-mathgroup at wri.com

> That said, I have one small (2 MB) file I've been using as a test case and 
[sample data snipped]
> 
> After some fussing, I discovered that 
> 
>    ReadList[deflectionFile,Real,
>    	RecordLists -> True,
>    	RecordSeparators -> {"\r"}]
> 
> will read in the file. Alas, I cannot (nor, do I want to) handle the entire 
> file in one humongous chunk.

Actually, there is a way to get ReadList to do this.  Try the following:

strm = OpenRead[deflectionFile]
ReadList[strm, Real, n,
	RecordLists -> True,
	RecordSeparators -> {"\r"}]

This will read n numbers (note--*not* n records, but n numbers) from the
stream and leave the stream pointer so that the next Read or ReadList
will begin where this one left off.  So, if you wanted to process this
in groups of 30, just plug in 30 for n, perform your ReadList, process
the data, perform another ReadList, and so on.

> Thus, we try the approach...
> 
>    In[43]:=
>       strm = OpenRead[deflectionFile]
>       Read[strm,Real]
>       Read[strm,Real]
>       Read[strm,Real]
>       Read[strm,Real]
>       Close[strm]
>    
>    Out[43]=
>       InputStream[test15.mts, 9]
>    Out[44]=
>       97.957191
>    Out[45]=
>       -1.1539974
>    Out[46]=
>       -9207.8301
>    Out[47]=
>       EndOfFile
>    Out[48]=
>       test15.mts
>    
> along with the error message:
> 
>    From In[43]:=
>       Read::readn: Syntax error reading a real number from test15.mts.

I note that RecordSeparators in your ReadList was "\r".  Thus, I suspect
that your file was no ordinary Unix text file, as a Unix text file would
be using "\n" for line separation.  This is the key to why Read[] isn't
working.  Although Read accepts the RecordSeparators option, it doesn't
work the same way as it does in ReadList with respect to the reading of
Reals.  In ReadList, RecordSeparators works if you're reading the
Record type (just like the Real and String types, there is also a Record
type), or if you have RecordLists->True.  Read can read the Record type,
but has no notion of RecordLists since it only reads one thing at a time.

While my earlier solution of limiting ReadList is really the best solution
to the problem, you may be wondering how we would go about reading the
numbers using Read[].  I would probably do the following, myself:

strm=OpenRead[deflectionFile];
foo=StringToStream[Read[strm,Record,RecordSeparators->"\r"]]
numlist=ReadList[foo,Number]
Close[foo]

You can repeat the last three lines however many times you want.

Incidentally, if you wanted to not worry about record separators, you
could get rid of the \r's.  If you got this from a DOS system, you can
do:

perl -p -i -e "s/\r//g;" filename

Or, if you got this file from a Mac, you could do:

perl -p -i -e "s/\r/\n/g;" filename

If you don't have perl, you could do it in sed, too, but I have no idea
what exactly you would need to type in.

The three argument form of ReadList is documented on page 494 of the
Mathematica book.

> Thanks for any help,
> 
> Mark.
> -- 
> Dr. Mark Kotanchek
> Signal Processing Dept - 363 ASB
> Applied Research Lab/Penn State
> P.O. Box 30
> State College, PA 16804

John Fultz
Applications Group
Wolfram Research, Inc.

==== [MESSAGE SEPARATOR] ====


  • Prev by Date: Re: Two questions regarding command line interface
  • Next by Date: Re: TRYING to read ASCII data with Mathematica
  • Previous by thread: TRYING to read ASCII data with Mathematica
  • Next by thread: Re: TRYING to read ASCII data with Mathematica