Search All of the Math Forum:
Views expressed in these public forums are not endorsed by
NCTM or The Math Forum.



Re: [mg4228] Re: help! to input data...
Posted:
Jun 26, 1996 2:59 AM


> In article <4pit53$jk5@dragonfly.wolfram.com>, tcdoe+@pitt.edu says... > > > >well, silly me. > >I thought i was pretty good at the basics of mathematica, but today i > >tried to input some numerical data that was comma delimited. i.e. > > > >456,45,21,0,5 > >43,25,3,66,65 > >... > >... > >... > > > >it is a large data file (10Mb+) with 5 numbers per line. i can't > >convert the commas to tabs easily. > > You don't need to convert the commas to tabs. > > Solution #1: Open the file and put a "{" before the first number > and a "}" after the last. This creats one big list and can be > read in using something like data = <<"C:\\temp\\data.txt";
BAD!! Don't do this! You will get erroneous data and Mathematica will never tell you there were any errors. Here's what happens. Create the sample data file:
{1,2,3,4 5,6,7,8}
Now:
<<"d:\\tmp\\foo" {1, 2, 3, 20, 6, 7, 8}
There was no comma at the end of the first line, so Mathematica took this as implicit multiplication. You'll get the wrong data in and never be the wiser unless you verified the data.
A correct solution for the original data file...that is, something of the form:
1,2,3,4 5,6,7,8
could be:
Map[First, ReadList["d:\\tmp\\foo", {Number, Character}]]
A more general solution is:
ToExpression[ReadList["d:\\tmp\\foo", Word, WordSeparators>{","}]]
An even more general solution which would allow you to read data in rows and thus preserve the 2dimensionality is:
ToExpression[ReadList["d:\\tmp\\foo", Word, WordSeparators>{","}, RecordLists>True]]
However, if memory serves me correctly, the first solution is somewhat faster if it suits your purpose.
Sincerely,
John Fultz jfultz@wolfram.com Applications Group Wolfram Research, Inc.



