Re: Changing String to Real
- To: mathgroup at smc.vnet.net
- Subject: [mg41114] Re: Changing String to Real
- From: Bill Rowe <listuser at earthlink.net>
- Date: Sat, 3 May 2003 03:29:51 -0400 (EDT)
- Sender: owner-wri-mathgroup at wolfram.com
On 5/2/03 at 3:58 AM, lwalker701 at earthlink.net (Lawrence A. Walker Jr.)
wrote:
>I propose another that works for all types of numbers and avoids the
>need to 'count characters'.
>It only require that there is some consistent format to the data.
>For instance, assuming that a number is always separated from a string
>by ':' then we can use the following construct.
>s = StringReplace["Uoc:0.752", ":" -> " "];
>(* string should have no other spaces *)
>str = StringToStream[s];
>num = ReadList[str, {Word, Number}][[1]];
>Close[str];
>num[[1]]
While using StringReplace to replace the ":" works, it is not necessary. An alternative would be:
str=StringToStream[s];
num=ReadList[str, {Word, Number}, RecordSeparators->{":","\n"}][[1]];
The RecordSeparators options allow a lot of flexibility for reading files with fixed formats.