How do I cut out a piece of text?

Greg Landrum glandrum at my-deja.com
Thu Sep 14 09:45:42 EDT 2000


In article <8pq0kg$d20$1 at nyheter.chalmers.se>,
  "Fredrik Lundin" <m94lufr at mtek.chalmers.se> wrote:
> Hey!
>
> I want to cut out a piece of text and put it in a new file.
> Lets say that the text looks like this (VRML transform nodes):
>
>   DEF faceA Transform {
>    children Shape {
>     appearance Appearance { material USE Testobjekt_0 }
>     geometry USE faceA_0Geo
>    }
>   }
>   DEF faceB Transform {
>    children Shape {
>     appearance Appearance { material USE Testobjekt_0 }
>     geometry USE faceB_0Geo
>    }
>   }
>   DEF faceC Transform {
>    children Shape {
>     appearance Appearance { material USE Testobjekt_0 }
>     geometry USE faceC_0Geo
>    }
>   }
>
> I would like to put this plart into a new file:
>
>   DEF faceB Transform {
>    children Shape {
>     appearance Appearance { material USE Testobjekt_0 }
>     geometry USE faceB_0Geo
>    }
>   }
>
> Could someone help me with this?
> I have tried quite alot to accomplish it, but I can't seem to
eliminate the
> stuff ahead of the wanted text portion.
>

Here's one solution which uses the re module and your advance knowledge
of what signals the beginning and end of the block you want:

#-------------
import re

def GetCutLines(lines,startStr,endStr):
    startRE = re.compile(r'\ *'+startStr)
    endRE = re.compile(r'\ *'+endStr)
    res = []
    pos = 0
    # find the start of the block
    while pos < len(lines):
        if startRE.match(lines[pos]) is not None:
            res.append(lines[pos])
            break
        pos = pos + 1
    pos = pos+1
    # find the end of the block
    while pos < len(lines):
        if endRE.match(lines[pos]) is not None:
            break
        else:
            res.append(lines[pos])
        pos = pos + 1
    return res


if __name__ == '__main__':
    f = open('test_cut.txt','r')
    l = f.readlines()
    res = GetCutLines(l,'DEF faceB Transform','DEF ')
    o = open('test_cut.out.txt','w+')
    o.writelines(res)
#-------------
It is certainly possible to make this fancier and/or faster, but this
does accomplish what you were asking for.

I hope this helps,
-greg


Sent via Deja.com http://www.deja.com/
Before you buy.



More information about the Python-list mailing list