Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.soft-sys.math.mathematica > #3437

Re: Reading Data Table

From Bill Rowe <readnews@sbcglobal.net>
Newsgroups comp.soft-sys.math.mathematica
Subject Re: Reading Data Table
Date 2011-07-01 08:53 +0000
Organization Steven M. Christensen and Associates, Inc and MathTensor, Inc.
Message-ID <iuk1tp$rp2$1@smc.vnet.net> (permalink)

Show all headers | View raw


On 6/30/11 at 6:30 AM, trchan@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


Back to comp.soft-sys.math.mathematica | Previous | Next | Find similar | Unroll thread


Thread

Re: Reading Data Table Bill Rowe <readnews@sbcglobal.net> - 2011-07-01 08:53 +0000

csiph-web