Re: Reading tables with labels
- To: mathgroup at smc.vnet.net
- Subject: [mg5544] Re: Reading tables with labels
- From: "David B. Wagner" <dbwagner at princon.com>
- Date: Thu, 19 Dec 1996 01:02:37 -0500
- Organization: Principia Consulting
- Sender: owner-wri-mathgroup at wolfram.com
William R. Pearson wrote: > > I would like to read a table that looks like this: > > name len lamr lamv kr kv > FEPE 54 0.2316 0.1544 0.4582 0.2030 > H3NJ1W 60 0.2136 0.1826 0.1589 0.1244 > NTSRIA 64 0.2087 0.1614 0.1730 0.1117 > N2KF1U 74 0.2115 0.1560 0.1416 0.0826 > LWBOA 75 0.1774 0.1910 0.0335 0.0352 > LWPMA 81 0.1797 0.1809 0.0375 0.0378 > > Into two lists, one with the name of the column and a second with > the actual values of the columns. I can read the data with ReadList[], > but of course it chokes on the initial labels. You need to open a stream to the file, read the label row using the Read command, and then use ReadList to snarf up the remainder of the table. E.g., s = OpenRead["yourdatafile"]; labels = Read[s, String]; data = ReadList[s, your_format_specification]; The general rule is, you can call Read as many times as you want, but as soon as you call ReadList, everything up to the end of the file is read. Note that there's no obvious way to separate the individual strings in the label line. I have used the following approach: s = OpenRead["yourdatafile"]; s2 = StringToStream[Read[s, String]]; labels = ReadList[s2, Word]; data = ReadList[s, your_format_specification]; StringToStream creates a stream from a string, which means you can then use ordinary Read/ReadList operations on it. It is analogous to using sscanf (as opposed to scanf) in C. Has anybody got a better way to do this, in situations where you don't know the number of labels in advance? -- Dave Wagner Principia Consulting http://www.princon.com/princon/ Tel: (500) PRN-CPIA