Re: ReadList, mix Words & Numbers?
- To: mathgroup at smc.vnet.net
- Subject: [mg30890] Re: ReadList, mix Words & Numbers?
- From: "Mariusz Jankowski" <mjkcc at usm.maine.edu>
- Date: Sun, 23 Sep 2001 02:16:30 -0400 (EDT)
- Organization: University of Southern Maine
- References: <9oetdp$im0$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
"Robert Love" <rlove at neosoft.com> wrote in message news:9oetdp$im0$1 at smc.vnet.net... > I'm trying to read a text file, the so called Yuma format of the GPS > Almanac. Each entry is 15 lines line, the last being a blank and the > first being a string I don't care about. I want to build lists of > numbers from the file. I see how to read whole records but can't find > how to extract just the numbers I want. > > > My general question is: How do I read a line of mixed text and numbers and > extract just the numbers? All advice appreciated. > > Robert, here is the solution. I used the following approach - read the data as a list of records and use string operations to locate the numbers and extract. In[19]:= rawdata = Flatten[ReadList["almanac.txt", {Record}]] ; This deletes the header records In[21]:= tmp = Select[rawdata, !StringMatchQ[#1, "*almanac*"] & ] Here I search each string for the character ":" and extract the number In[34]:= values = (ToExpression[StringDrop[#1, First[Flatten[StringPosition[#1, ":"]]]]] & ) /@ tmp Out[34]= {1, 0, -0.6150340397452159, 319488., 0.9654515552, -10.072193720509482, 5153.727051, 1.770381612241071, -1.702684969, 0.6520090636422935, -2.4944906649096104, 0., 99, 2, 0, -0.4199863658579357, 319488., 0.9324469998, -10.224423843839208, 5153.597656, 0.6048908201090217, -2.027413647, 1.537113200919309, -5.244330671054458, -12.977810336677443, 99} Similarly, I extract the label In[35]:= labels = (StringTake[#1, First[Flatten[StringPosition[#1, ":"]]]] & ) /@ tmp Out[35]= {"ID:", "Health:", "Eccentricity:", "Time of Applicability(s):", "Orbital Inclination(rad):", "Rate of Right Ascen(r/s):", "SQRT(A) (m 1/2):", "Right Ascen at Week(rad):", "Argument of Perigee(rad):", "Mean Anom(rad):", "Af0(s):", "Af1(s/s):", "week:", "ID:", "Health:", "Eccentricity:", "Time of Applicability(s):", "Orbital Inclination(rad):", "Rate of Right Ascen(r/s):", "SQRT(A) (m 1/2):", "Right Ascen at Week(rad):", "Argument of Perigee(rad):", "Mean Anom(rad):", "Af0(s):", "Af1(s/s):", "week:"} Here I display the result In[38]:= Transpose[{labels, values}] // TableForm Good luck, Mariusz -- ====================================================== Mariusz Jankowski University of Southern Maine mjkcc at usm.maine.edu