Python Regex Question
Terry Reedy
tjreedy at udel.edu
Wed Oct 29 17:16:41 EDT 2008
MalteseUnderdog wrote:
> Hi there I just started python (but this question isn't that trivial
> since I couldn't find it in google :) )
>
> I have the following text file entries (simplified)
>
> start #frag 1 start
> x=Dog # frag 1 end
> stop
> start # frag 2 start
> x=Cat # frag 2 end
> stop
> start #frag 3 start
> x=Dog #frag 3 end
> stop
> ....
>
> I need a regex expression which returns the start to the x=ANIMAL for
> only the x=Dog fragments so all my entries should be start ...
> (something here) ... x=Dog . So I am really interested in fragments 1
> and 3 only.
As I understand the above....
I would first write a generator that separates the file into fragments
and yields them one at a time. Perhaps something like
def fragments(ifile):
frag = []
for line in ifile:
frag += line
if <line ends fragment>:
yield frag
frag = []
Then I would iterate through fragments, testing for the ones I want:
for frag in fragments(somefile):
if 'x=Dog' in frag:
<do whatever>
Terry Jan Reedy
More information about the Python-list
mailing list