[Tutor] Iterating over a list for a certain character...

alan.gauld@bt.com alan.gauld@bt.com
Fri, 14 Dec 2001 11:26:24 -0000


> ok, i have read a file into a list of strings, and i need to 
> copy out all of the strings that contain a certain 
> character, 

Try the string.find() function or if its more complex the re.search()
function.

> sort them, 

list.sort()

You can provide your own comparison function too.


> and print them.

print

However there is a module designed to make this kind 
of thing easier, its called:

fileinput

And allows you to do:

import fileinput
for line in fileinput.input():
    process(line)

So it handles all the file opening etc. It also allows 
you to process multiple files as if they were one big 
one. Which might help you...

Finally you might like to read my file handling topic
on my web tutor

Alan g.
Author of the 'Learning to Program' web site
http://www.freenetpages.co.uk/hp/alan.gauld