[Tutor] Searching in a file

spir denis.spir at free.fr
Thu Jan 14 13:25:03 CET 2010

On Wed, 13 Jan 2010 23:05:11 -0000
"Alan Gauld" <alan.gauld at btinternet.com> wrote:

> But a third option is to use a split and apply it to the whole file as
> a string thereby breaking the file into as many chunks as start with
> a line containing 'NEW'...

Why not simply a regex pattern starting with "NEW" and ending with '\n'?

import re
pat = re.compile("""NEW(.+)\n""")
source = "abc\defNEWghi\njkl\nmno\nNEWpqr\nstu\n"
print pat.findall(source)
# ==> ['ghi', 'pqr']


la vita e estrany


More information about the Tutor mailing list