Parsing multiple lines from text file using regex

Marc marc at marcd.org
Sun Nov 3 20:28:04 EST 2013


>        This is an alternative solution someone else posted on this list for a similar problem I had:

 

>        #!/usr/bin/python3

>        from itertools import groupby

>        def get_lines_from_file(file_name):

>        with open(file_name) as reader:

>        for line in reader.readlines():

>        yield(line.strip())

 

>        counter = 0

>        def key_func(x):

>        if x.strip().startswith("banner") and x.strip().endswith("<banner text delimiter>"):

>        global counter

>        counter += 1

>        return counter

 

>        for key, group in groupby(get_lines_from_file("my_data"), key_func):

>        print(list(group)[1:-1])

 

 

 

Thanks Jason,

 

banner = re.compile(r'banner\s+(\w+)\s+(.+)(.*?)\2', re.DOTALL).findall(lines)

 

worked nicely to get what I needed:

 

outfile.write("Banner type: %s     Banner Delimiter: %s\n" % (banner[0][0], banner[0][1]))
outfile.write("Banner Text:\n")
outfile.write(banner[0][2])

 

Probably not the prettiest, most concise code, but it gets the job done.

 

Thanks again,

Marc

 

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20131103/0812a35f/attachment.html>


More information about the Python-list mailing list