MathGroup Archive 2011

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

Search the Archive

Re: Reading Data Table

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

>For example, a file containing "1 2 3\n4 5 6" would become {{1,2,3},

>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


  • Prev by Date: Re: Remote presentation clicker for mathematica slideshows.
  • Next by Date: Re: How to write a "proper" math document
  • Previous by thread: Re: Remote presentation clicker for mathematica slideshows.
  • Next by thread: Font and Style problems using Image to create bitmaps of Plots/Charts etc