MathGroup Archive 2005

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

Search the Archive

Re: Importing tab-delimited data files?

  • To: mathgroup at smc.vnet.net
  • Subject: [mg62634] Re: Importing tab-delimited data files?
  • From: albert <awnl at arcor.de>
  • Date: Wed, 30 Nov 2005 00:06:13 -0500 (EST)
  • References: <dmh9o2$908$1@smc.vnet.net>
  • Sender: owner-wri-mathgroup at wolfram.com

Hi,

> (The columns should line up if your reader uses monospaced type.)
> 
> 11   aaa  22   bbb  33   ccc
>               22   bbb  33   ccc
>                             33   ccc
> 
> Opening Mathematica and using  !!filedata  reproduces exactly same thing:
>  
> 11   aaa  22   bbb  33   ccc
>               22   bbb  33   ccc
>                             33   ccc
> 
> Trying to follow this with
> 
>    fileDataAsViewed = !!fildata
> 
> or
> 
>    fileDataAsViewed = %
> 
> doesn't work, however.


no, !! is somewhat special. But you should be able to get a string
containing the whole file content anyway, e.g. by 

filecontentasstring = Import[filename,"Text"]

> Using the Mathematica expression
> 
>    Import["datafile", "Table",
>       ConversionOptions->{"TableSeparators"->{{"\r","\n"},{"\t"}}}]
> 
> gives:
> 
> 11   aaa  22   bbb  33   ccc
> 22   bbb  33   ccc
> 33   ccc
> 
> Recreating the text file with a space between the tabs in the empty
> slots and applying the same  Import[ ]  expression, however, gives the
> "right" answer:
> 
> 11   aaa  22   bbb  33   ccc
>               22   bbb  33   ccc
>                             33   ccc
> 
> I suppose this is not exactly unexpected.  The problem is, the app that
> creates the (much larger) tab-delimited filedata text file I really want
> to load into a Mathematica Table creates numerous blank cells, i.e.
> adjacent and unspaced tabs.  I guess I'll just have to go at it with a
> smart text editor and separate adjacent tabs before trying to load it.

You won't need to do that, my suggestion would be to Read line by line and
split the lines with StringSplit:

tablewithemptystrings = StringSplit[#,"\t"]&/@ReadList[datafile,"String"]

this will give you a table of strings, empty slots will be empty strings,
"", that is. Maybe you need to convert the other entries to numbers etc. 

hth

albert


  • Prev by Date: Re: 3D plot question
  • Next by Date: Re: Random Normal deviates within compiled function?
  • Previous by thread: Re: Importing tab-delimited data files?
  • Next by thread: FindMinimum is not obeying my bounds