Re: Reading Data Table
- To: mathgroup at smc.vnet.net
- Subject: [mg119956] Re: Reading Data Table
- From: Bill Rowe <readnews at sbcglobal.net>
- Date: Fri, 1 Jul 2011 04:50:15 -0400 (EDT)
On 6/30/11 at 6:30 AM, trchan at mit.edu (fractal11) wrote:
>Is there a way to read a file with a user specified word separator
>and record separator, such that the result is a list of lists of
>words?
>For example, a file containing "1 2 3\n4 5 6" would become {{1,2,3},
>{4,5,6}}.
>Import does almost exactly what I want, although I can't figure out
>how to change the word and record separators.
>In particular, I have a file where word separators are tab
>characters, but the data themselves have spaces. Therefore,
>Mathematica's default word separator of whitespace does not work.
For a ASCII file containing *only* numeric data and whitespace,
the most efficient way I know to read the data would be
ReadList[filename, Number, RecordLists->True]. For example:
In[1]:= strData = "1 2 3\n4 5 6";
ReadList[StringToStream[strData], Number, RecordLists -> True]
Out[2]= {{1, 2, 3}, {4, 5, 6}}
Note, it is possible to read this same file format with Import
as demonstrated by:
In[3]:= Import[StringToStream[strData], "Table"]
Out[3]= {{1, 2, 3}, {4, 5, 6}}
But ReadList will be faster for larger files since it will not
try to interpret what is being read as anything but a number. If
there is non-numeric data, the code above with ReadList will
generate an error. Import will not fail with an error if there
is non-numeric data. But the price is execution speed which can
be significant for large files.
Also, ReadList will allow for mixed data types if the file has a
very structured format that you can specify.
If you need to modify the word separators, record separators
from the default with ReadList, see
tutorial/ReadingTextualData