Re: Re: help! to input data...
- To: mathgroup at smc.vnet.net
- Subject: [mg4282] Re: [mg4228] Re: help! to input data...
- From: John Fultz <jfultz>
- Date: Sat, 29 Jun 1996 03:53:57 -0400
- Sender: owner-wri-mathgroup at wolfram.com
> In article <4pit53$jk5 at dragonfly.wolfram.com>, tcdoe+ at 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 2-dimensionality 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 at wolfram.com Applications Group Wolfram Research, Inc.