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: [mg94593] Re: [mg94431] Import numerical data and split by pattern
  • From: "Scot T. Martin" <smartin at deas.harvard.edu>
  • Date: Wed, 17 Dec 2008 06:32:37 -0500 (EST)
  • References: <200812121158.GAA28168@smc.vnet.net>

Here's one way to do it using StringPatterns.

In[1]:=
SetDirectory[NotebookDirectory[]];
data = Import["data.txt"]

Out[2]=
"## 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"

In[3]:=
DeleteCases[#,_?(!NumberQ[#]&)]&/@(ToExpression/@
StringSplit[#,"  "|"\n"]&@StringCases[data,
Shortest["##"~~items__~~"#"|EndOfString]->items])

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



> On Fri, 12 Dec 2008, 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?
>> With thanks in advance
>> Yours Wolfgang
>> 
>> 
>


  • Prev by Date: Re: Plot and filling option
  • Next by Date: Re: Some Problem With 7.0.0
  • Previous by thread: Re: Import numerical data and split by pattern
  • Next by thread: Re: Import numerical data and split by pattern