Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2005
*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 2005

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

Search the Archive

Re: String to numbers

  • To: mathgroup at smc.vnet.net
  • Subject: [mg55355] Re: String to numbers
  • From: Peter Pein <petsie at arcor.de>
  • Date: Sun, 20 Mar 2005 04:11:47 -0500 (EST)
  • References: <d1gu3b$p0$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Erb, John 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
> 
> 
Hi John,

a small test file:

!!data.txt
<2.54   3.11   2.89>
<3.58   5.24   5.66>

The middle elements will be imported correctly, but the outer ones will
be strings. So

Import["data.txt", "Table"] /. s_String :>
   ToExpression[StringReplace[s, "<" | ">" -> ""]]

gives you

{{2.54, 3.11, 2.89}, {3.58, 5.24, 5.66}}

just as test:
Map[Head, %, {-1}]
{{Real, Real, Real}, {Real, Real, Real}}

Worked fine.

-- 
Peter Pein
Berlin


  • Prev by Date: Re: filling in an array
  • Next by Date: Re: a simplification problem
  • Previous by thread: Re: String to numbers
  • Next by thread: Re: String to numbers