Mathematica 9 is now available
Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2002
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*November
*December
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2002

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re:trying to pull numbers out of a string from a file...

  • To: mathgroup at smc.vnet.net
  • Subject: [mg36624] Re:[mg36595] trying to pull numbers out of a string from a file...
  • From: "tgarza01 at prodigy.net.mx" <tgarza01 at prodigy.net.mx>
  • Date: Fri, 13 Sep 2002 23:33:36 -0400 (EDT)
  • Sender: owner-wri-mathgroup at wolfram.com

Hello, Rob:
First of all, I used ReadList directly, with Word instead of String, and
with the option WordSeparators -> None, like this (I presume your file is
adequately located, so that there is no problem in finding it):

In[1]:=
a = ReadList["197-tst.txt", Word, RecordLists -> True, WordSeparators ->
None];

This allowed me to examine your records and I found out that in this way
each record comes out as a list of length 1:

 In[2]:=
Head[a[[1]]]
Out[2]=
List


In[3]:=
Length[a[[1]]]
Out[3]=
1

That is,


In[3]:=
a[[1]]
Out[3]=
{"aEX-004   2002197 0 0 0 5935.80 5946.66  27.06 -1281.9   -229.    321.   
317.    367.   -115.    126.    146.   -410.   \
-426.000000EF   75."}

In[4]:=
StringLength[a[[1,1]]]
Out[4]=
140


and the characters you want are

In[5]:=
StringTake[a[[1,1]], {25, 110}]
Out[5]=
"5935.80 5946.66  27.06 -1281.9   -229.    321.    317.    367.   -115.   
126.    146."

So far, so good. It seems that you want these 11 numbers, OK? The problem
now, I think, is that this is just a string and I can think of no easy way
to convert it precisely into a list of 11 real numbers. Then, I suggest you
read the file in a different way, without the WordSeparators option:

In[6]:=
b=ReadList["197-tst.txt",Word,RecordLists -> True];

In[7]:=
b[[1]]

Out[7]=
{aEX-004,2002197,0,0,0,5935.80,5946.66,27.06,-1281.9,-229.,321.,317.,367.,-\
115.,126.,146.,-410.,-426.000000EF,75.}

In[8]:=
Head[b[[1]]]
Out[8]=
List

In[9]:=
Length[b[[1]]]
Out[9]=
19

so that each record is now a list of 19 strings. What you want is strings 6
to 16, but converted to reals (unless I'm being presumptuous). This will
achieve that:

In[10]:=
ToExpression[Take[b[[1]],{6,16}]]
Out[10]=
{5935.8,5946.66,27.06,-1281.9,-229.,321.,317.,367.,-115.,126.,146.} 

Now you have a nice list of real numbers to work with. You can do this for
the whole file like this:

In[11]:=
ToExpression[Take[#,{6,16}]&/@b]; 

I hope this will solve your problem.

Tomas Garza
Mexico City


> ----- Original Message -----
> From: "1.156" <rob at piovere.com>
To: mathgroup at smc.vnet.net
> Sent: Friday, September 13, 2002 12:14 AM
> Subject: [mg36624] [mg36595] trying to pull numbers out of a string from a file...
>
> > I've got to extract some numbers from a file that are in lines of text.
> Since the line contents are not numbers, I presume I must pull the line
out
> as a string.  Here I start by pulling out just one line:
> >
> > inFile = OpenRead["197-tst.txt"]
> > y = ReadList[inFile, String, 1, RecordLists -> True]
> > Close[inFile];
> >
> > This appears to pull in a line. Now I want to take characters 25 to 110
to
> get just the stuff I want:
> > y1=StringTake[y ,{25,110}];
> >
> > Here's the output.  StringTake doesn't seem to work.
> >
> > StringTake[{aEX-004   2002197 0 0 0 5935.80 5946.66  27.06 -1281.9  
-229.
> 321.    317.    367.   -115.    126.    146.   -410.  -426.000000EF  
75.},
> {25, 110}]
> >
> > It doesn't take loading another package as far
> > as I can tell from the help.  I'm thinking that it doesn't work because
> it's trying to work on a list
> > rather than a string. I've tried Flatten, and other stuff to try to get
to
> just a string and not a list but
> > nothing has worked so far.  I'm a long way from getting to those numbers
> in there but heck, I
> > can't even get to the string.  Can anyone point me in the right
direction?
> >
> > Thanks, Rob
> >





--------------------------------------------------------------------
mail2web - Check your email from the web at
http://mail2web.com/ .




  • Prev by Date: Re: MathematicaTeX
  • Next by Date: Re: Empirical CDF and InterpolatingFunction
  • Previous by thread: Re: trying to pull numbers out of a string from a file...
  • Next by thread: More levels of subsection nesting