Newbie help with manipulating text files

Tom Good Tom_Good1 at excite.com
Fri May 25 19:00:42 EDT 2001


"jk" <user at host.com> wrote in message news:<20010525.181114.1143408282.868 at dsl-64-194-28-125.telocity.com>...
> I hope someone can help me with this problem, I know I am just thinking
> about it the wrong way.
> 
> I have a file that I want to divide into three parts: a header, a section
> I want to sort, and a footer. The header and footer are not in a
> consistent format, but the middle part I want to sort has a bunch of
> lines that all start with the same word. 

After looking at my first solution a while longer -- which, no doubt,
I should have done before posting it :-) -- I like this shorter one
better:

import re
import string

regex_for_midsection = "^middle"

inputLines = """
header header header this is a header
this is a headerthis is a headerthis is a header
this is a headerthis is a headerthis is a header
yep this is a header

middle part z
middle part a
middle part c
middle part d
middle part e

footer this is the footerfooter this is the footerfooter this is the
footerfooter this is the footerfooter this is the footerfooter this is
the
footerfooter this is the footerfooter this is the footerfooter this is
the
footerfooter this is the footerfooter this is the footerfooter this is
the
footerfooter this is the footerfooter this is the footer

"""

def until(func, L):
    for i in range(len(L)):
        if func(L[i]):
            return L[:i]
    return L

def after(func, L):
    for i in range(len(L)-1, -1, -1):
        if func(L[i]):
            return L[i+1:]
    return L 

def main():
    lines = string.split(inputLines, "\n")
    
    def matchesMid(line):
        return (re.findall(regex_for_midsection, line) != [])

    start = until(matchesMid, lines)
    mid = filter(matchesMid, lines)
    mid.sort()
    end = after(matchesMid, lines)

    for L in start + mid + end:
        print L

if __name__=="__main__":
    main()



More information about the Python-list mailing list