question re file as sequence

Paul Rubin http
Sat Jul 10 19:50:26 EDT 2004


I have a file with contents like:

   Vegetable:
   spinach
   Fruit:
   banana
   Flower:
   Daisy
   Fruit:
   pear

I want to print out the names of all the fruits.  Is the following valid?

   f = open(filename) 
   for line in f:
      if line == 'Fruit:\n':
         print f.readline()

The issue here is that the sequence has been mutated inside the loop.

If the above isn't good, anyone have a favorite alternative to doing
it like that?

Thanks.



More information about the Python-list mailing list