deleting texts between patterns

Ravi Teja webraviteja at gmail.com
Fri May 12 04:11:08 EDT 2006


mickle... at hotmail.com wrote:
> hi
> say i have a text file
>
> line1
> line2
> line3
> line4
> line5
> line6
> abc
> line8 <---to be delete
> line9  <---to be delete
> line10  <---to be delete
> line11  <---to be delete
> line12  <---to be delete
> line13 <---to be delete
> xyz
> line15
> line16
> line17
> line18
>
> I wish to delete lines that are in between 'abc' and 'xyz' and print
> the rest of the lines. Which is the best way to do it? Should i get
> everything into a list, get the index of abc and xyz, then pop the
> elements out? or any other better methods?
> thanks

In other words ...
lines = open('test.txt').readlines()
for line in lines[lines.index('abc\n') + 1:lines.index('xyz\n')]:
    lines.remove(line)
for line in lines:
    print line,

Regular expressions are better in this case
import re
pat = re.compile('abc\n.*?xyz\n', re.DOTALL)
print re.sub(pat, '', open('test.txt').read())




More information about the Python-list mailing list