Re: String to numbers
- To: mathgroup at smc.vnet.net
- Subject: [mg55397] Re: String to numbers
- From: Maxim <ab_def at prontomail.com>
- Date: Mon, 21 Mar 2005 03:02:04 -0500 (EST)
- References: <d1gu3b$p0$1@smc.vnet.net>
- Sender: owner-wri-mathgroup at wolfram.com
On Sat, 19 Mar 2005 10:15:07 +0000 (UTC), Erb, John <jerb at saint-lukes.org>
wrote:
>
> Hello,
>
> I have data in file in the form of strings, formatted as:
>
> <2.54 3.11 2.89>
> <3.58 5.24 5.66>
> ... more
>
> How can I read this into mathematica, and convert the strings to
> numbers,
> so I would have
>
> {{2.54,3.11,2.89},{3.58,5.24,5.66},...{}}
>
> Thank you,
>
> John C. Erb
> Email: John_C_Erb at prodigy.net
>
If we want to avoid using ToExpression, then we can do
In[1]:=
ReadList["/data1", Record,
RecordSeparators -> {"<", ">", "\n"}] //
ReadList[StringToStream[#], Number]& /@ #&
Out[1]=
{{2.54, 3.11, 2.89}, {3.58, 5.24, 5.66}}
but I don't understand why
ReadList["/data1", {Number, Number, Number},
RecordSeparators -> {"<", ">", " ", "\n"}]
doesn't work. According to the description of the option NullRecords, the
empty record terminated by "<" at the beginning of the file just should be
skipped. Or, taking a simpler example, if the file data2 contains 1::2,
then
ReadList["/data2", Number,
RecordSeparators -> {":"}]
should return {1, 2} but it gives {1, $Failed}. Besides,
ReadList["/data2", Number,
RecordSeparators -> {"::"}]
works but
ReadList["/data2", {Number},
RecordSeparators -> {"::"}]
again reads only the first element.
Maxim Rytin
m.r at inbox.ru