Re: Reading a file
- To: mathgroup at smc.vnet.net
- Subject: [mg25509] Re: [mg25475] Reading a file
- From: Tomas Garza <tgarza01 at prodigy.net.mx>
- Date: Thu, 5 Oct 2000 23:50:27 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
What kind of file is it? If you have it in an Excel table, for example (and if you don't, I suggest you put it there for ease of use), then save it as tab-delimited text and close it without changing format. Suppose you call it "sample.txt". I read it using In[1]:= newFile=ReadList["c:\Math_ag\sample.txt", Word, RecordLists -> True] Out[1]= {{"s", "1", "even", "21", "2.1", "40"}, {"p", "1", "even", "7", "2.1", "3"}, {"d", "1", "even", "5", "2.1", "3"}, {"f", "1", "even", "3", "2.1", "3"}} As you can see all the elements are now strings, since the input file was a text file, and you needn't specify word separators. In order to use each element properly, convert the strings to expressions: In[2]:= newerFile=ToExpression /@ Cases[newFile, x_, {2}] Out[2]= {s, 1, even, 21, 2.1, 40, p, 1, even, 7, 2.1, 3, d, 1, even, 5, 2.1, 3, f, 1, \ even, 3, 2.1, 3} Now you see that: In[3]:= Head /@ newerFile Out[126]= {Symbol, Integer, Symbol, Integer, Real, Integer, Symbol, Integer, Symbol, \ Integer, Real, Integer, Symbol, Integer, Symbol, Integer, Real, Integer, \ Symbol, Integer, Symbol, Integer, Real, Integer} Finally, regroup properly: In[4]:= lastFile=Partition[newerFile, 6] Out[4]= {{s, 1, even, 21, 2.1, 40}, {p, 1, even, 7, 2.1, 3}, {d, 1, even, 5, 2.1, 3}, {f, 1, even, 3, 2.1, 3}} Tomas Garza Mexico City "Rod" <rodolphe6831 at my-deja.com> wrote: > I'm trying to read the following file, > > s,1,even,21,2.1,40 > p,1,even,7,2.1,3.0 > d,1,even,5,2.1,3.0 > f,1,even,3,2.1,3.0 > > I want the field separator to be "," so i tried > ReadList["file",{Character,Number,Word,Number,Number,Number},WordSeparato rs-> > ","] but it does not seem to work. Any ideas ?