Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2011

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

Search the Archive

Re: Mathematica v8: import a space-delimited table of numbers?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg115852] Re: Mathematica v8: import a space-delimited table of numbers?
  • From: Peter Pein <petsie at dordos.net>
  • Date: Sat, 22 Jan 2011 03:25:16 -0500 (EST)
  • References: <ihbjoe$de1$1@smc.vnet.net>

On 21.01.2011 10:30, Gordon Robertson wrote:
> I routinely use Import[] for "CSV" and "TSV" files, but now need to
> import a space-delimited file in which each line has a string as the
> first element, and then thousands of (space-delimited) decimal numbers.
>
> After looking at the help documentation, and previous posts on
> MathGroup, I don't see a simple way to Import while specifying that the
> separator is a space (in R's read.table, I'd use sep=" ").
>
> I expect that I could read in the file as a table, split lines on
> spaces, then map ToExpression across the numbers in each row. Have I'd
> missed something in the documentation?
>
> Thanks for your help.
>
> Gordon

Hi Gordon,

  simpliy use an option; no need for ToExpression because Import will 
recognize strings and numbers:

In[1]:= 
FullForm[read=Import[ToFileName[{$HomeDirectory,"Desktop"},"test.txt"],"Table",
     "Separator"->" "]]
Out[1]//FullForm= 
List[List["first",1.2`,2.3`,3.4`,4.5`],List["second",9.8`,8.7`,7.6`,6.5`]]

and separate the numeric data with
data = Rest/@read
  or
data = read[[All,2;;]]

Cheers,
Peter
-------* copy of test.txt: *-------
first 1.2 2.3 3.4 4.5
second 9.8 8.7 7.6 6.5
------* end of copy *-------


  • Prev by Date: Re: log expression not simplified
  • Next by Date: Re: unexpected Timing results
  • Previous by thread: Mathematica v8: import a space-delimited table of numbers?
  • Next by thread: Re: The new (v.8.0) distribution plots using a numerical or date