[Tutor] how to print the match middle part out

Timo timomlists at gmail.com
Thu Dec 15 18:57:29 CET 2011


Op 15-12-11 17:52, lina schreef:
> Hi,
>
> For following:
>
> aaa
> model 0
> bbb
> acb
> model 1
> ccc
>
>
> How can I set regexp1 as "model 0" and end "model 1"
>
> so the final print out will be:
>
> model 0
> bbb
> acb
Just iterate over the lines (file or string) and retrieve the lines from 
the wanted line, until the line you're not interested in anymore. 
something like this:

data = """aaa
model 0
bbb
acb
model 1
ccc"""

result = []
first_reached = False
for line in data.split('\n'):
     if line == 'model 0':
         first_reached = True
     if line == 'model 1':
         break
     if first_reached:
         result.append(line)
print result

Cheers,
Timo

>
> above is a very simple example,
>
> Thanks for any advice,
>
> Best,
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> To unsubscribe or change subscription options:
> http://mail.python.org/mailman/listinfo/tutor



More information about the Tutor mailing list