Re: Import numerical data and split by pattern
- To: mathgroup at smc.vnet.net
- Subject: [mg94504] Re: [mg94431] Import numerical data and split by pattern
- From: "Scot T. Martin" <smartin at deas.harvard.edu>
- Date: Mon, 15 Dec 2008 07:44:10 -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 > >
- References:
- Import numerical data and split by pattern
- From: "Gehricht@googlemail.com" <Gehricht@googlemail.com>
- Import numerical data and split by pattern