[Tutor] extracting lines between patterns.

Bala subramanian bala.biophysics at gmail.com
Mon May 14 12:23:48 CEST 2012


The code is given below. Here i try to extract lines that are between the
two patterns atomtype and mol.type

#!/usr/bin/env python
import re
mypat=re.compile(r'^[ atomtypes ]$[ moleculetype ]',re.MULTILINE)
data=open('test.dat').read()
extr=re.findall(mypat,data)
print extr

The data file is something like the following,
[ defaults ]
; nbfunc        comb-rule       gen-pairs       fudgeLJ fudgeQQ
1               2               no              0.5     0.8333

[ atomtypes ]
;name   bond_type     mass     charge   ptype
 N3       N3          0.00000  0.00000        A
 H        H           0.00000  0.00000          A
 CT       CT          0.00000  0.00000       A
 HP       HP          0.00000  0.00000       A
 O2       O2          0.00000  0.00000       A

[ moleculetype ]
;name            nrexcl
 tripe                3


On Mon, May 14, 2012 at 9:53 AM, Russel Winder <russel at winder.org.uk> wrote:

> On Mon, 2012-05-14 at 09:31 +0200, Bala subramanian wrote:
> > Friends,
> > Could someone please give some hint on how to extract lines between two
> > patterns in a file. I use the re module to compile my patterns but not
> able
> > to figure out how i can use these patterns to extract the lines lying in
> > between.
>
> Without the source code you already have, it is difficult to provide any
> constructive suggestions.
>
> --
> Russel.
>
> =============================================================================
> Dr Russel Winder      t: +44 20 7585 2200   voip:
> sip:russel.winder at ekiga.net
> 41 Buckmaster Road    m: +44 7770 465 077   xmpp: russel at winder.org.uk
> London SW11 1EN, UK   w: www.russel.org.uk  skype: russel_winder
>



-- 
C. Balasubramanian
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120514/6ca45b58/attachment.html>


More information about the Tutor mailing list