deleting texts between patterns

bruno at modulix onurb at xiludom.gro
Fri May 12 05:56:20 EDT 2006


micklee74 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? 

Would be somewhat inefficient IMHO - at least for big files, since it
implies reading the whole file in memory.

> or any other better methods? 

Don't know if it's better for your actual use case, but this avoids
reading up the whole file:

def skip(iterable, skipfrom, skipuntil):
    """ example usage :
    >>> f = open("/path/to/my/file.txt")
    >>> for line in skip_print(f, 'abc', 'yyz'):
    >>>     print line
    >>> f.close()
    """
    skip = False
    for line in iterable:
        if skip:
            if line == skipuntil:
                skip = False
            continue
        else:
            if line == skipfrom:
                skip = True
                continue
        yield line

def main():
    lines = """
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
""".strip().split()
    for line in skip(lines, 'abc', 'xyz'):
        print line


HTH

-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list