ignore specific data

skip at pobox.com skip at pobox.com
Mon Nov 21 13:42:14 EST 2005


    pkilambi> I would like to ignore a block of lines and consider the
    pkilambi> rest..  so if the block starts with

    pkilambi> "start of block............."
    pkilambi> fjesdgsdhfgdlgjklfjdgkd
    pkilambi> jhcsdfskdlgjkljgkfdjkgj
    pkilambi> "end of block"

    pkilambi> I want to ignore this while processing the file .This block
    pkilambi> could appear anywhere in the file.It could at the start or end
    pkilambi> or even middle of file content.

How about (untested):

    class FilterBlock:
        def __init__(self, f, start, end):
            self.f = f
            self.start = start
            self.end = end

        def __iter__(self):
            return self

        def next(self):
            line = self.f.next()
            if line == self.start:
                line = self.f.next()
                while line != self.end:
                    line = self.f.next()
            return line

Then use it like

    filterfile = FilterBlock(open("somefile", "r"),
                             "start of block..........",
                             "end of block")

    for line in filterfile:
        process(line)

I'm not sure what you mean by all the dots in your start of block line.  If
"start of block" can be followed by other text, just use 

    if line.startswith(self.start):

instead of an exact comparison.

Skip



More information about the Python-list mailing list