Newbie help with manipulating text files

jk user at host.com
Sat May 26 03:11:14 CEST 2001


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. 

So the file looks something like this:


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

I've been able to use re to grab all of the middle part and put it into a
sortable list, but I want to be able to print out the whole file with the
middle part sorted but the header and footer unchanged. I think that what
I want is to read the whole thing into a list and then split the list
into 3 sublists, manipulate the list I want and then write out a new file
with the modified lists joined together. The part I don't know how to do
is to get the header and footer into their own lists.

thanks in advance,

jk



More information about the Python-list mailing list