MathGroup Archive 2008

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

Search the Archive

Re: Import numerical data and split by pattern

  • To: mathgroup at smc.vnet.net
  • Subject: [mg94483] Re: Import numerical data and split by pattern
  • From: Szabolcs Horvát <szhorvat at gmail.com>
  • Date: Sun, 14 Dec 2008 07:40:39 -0500 (EST)
  • Organization: University of Bergen
  • References: <ghtjl9$rfu$1@smc.vnet.net>

Gehricht at googlemail.com wrote:
> Hi!
> 
> I am asking this more out of curiosity, but is there a neat way to
> accomplish the following task within Mathematica.
> Suppose I have a file as follows
> ## a b c
> 1 1.0
> 2 2.0
> # d e f
> 3 3.0
> 4 4.0
> ## c d e
> 5 5.0
> 6 6.0
> ....
> Now I want everything between ## and # in a separate list, i.a. list1=
> {{1,1.0},{2,2.0}}, list2={{5,5.0},{6,6.0}}, etc.
> How could I get this?

I don't like to use Mathematica for such tasks because importing 
inhomogeneous data (mixed number types, strings, etc.) is slow, and 
Mathematica is not very good at text processing (parsing a special file 
format).

So if you have a lot of data, preprocess it with some other tool, and 
convert it to a format that Mathematica can process more easily.

There's no built-in way to only read the numbers that are between lines 
starting with ## and #, but not between # and ## ...

If you have little data and speed is not a problem then you can do 
something like this:

In[1]:= data = ImportString[
  "## a b c
   1 1.0
   2 2.0
   # d e f
   3 3.0
   4 4.0
   ## c d e
   5 5.0
   6 6.0 ", "Table"]

Out[1]= {{"##", "a", "b", "c"}, {1, 1.}, {2, 2.}, {"#", "d", "e",
   "f"}, {3, 3.}, {4, 4.}, {"##", "c", "d", "e"}, {5, 5.}, {6, 6.}}

In[2]:= Split[data, ! MatchQ[First[#2], "#" | "##"] &]

Out[2]= {{{"##", "a", "b", "c"}, {1, 1.}, {2, 2.}}, {{"#", "d", "e",
    "f"}, {3, 3.}, {4, 4.}}, {{"##", "c", "d", "e"}, {5, 5.}, {6, 6.}}}

In[3]:= Select[%, #[[1, 1]] == "##" &]

Out[3]= {{{"##", "a", "b", "c"}, {1, 1.}, {2, 2.}}, {{"##", "c", "d",
    "e"}, {5, 5.}, {6, 6.}}}

In[4]:= Rest /@ %

Out[4]= {{{1, 1.}, {2, 2.}}, {{5, 5.}, {6, 6.}}}





  • Prev by Date: Re: Sample uniformly from a simplex
  • Next by Date: Re: Import numerical data and split by pattern
  • Previous by thread: Re: Import numerical data and split by pattern
  • Next by thread: Re: Import numerical data and split by pattern