question re file as sequence

Christopher T King squirrel at WPI.EDU
Sun Jul 11 05:35:56 CEST 2004

On 10 Jul 2004, it was written:

> 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.

Not precisely -- 'for line in f' uses f as an iterator, not a sequence: it
asks for (and gets) one line at a time from f, rather than slurping the
whole thing in as a sequence and looping over it (like f.readlines() would 

Unfortunately, when f is used as an iterator, it seems to move the file
pointer to the end of the file (question for the higher ups: why? StringIO
doesn't seem to do this).  That's why f.readline() is returning ''.  To
get around this, you'll have to use one or the other method exclusively.  
Sticking with readline will probably be easiest:

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

The iter() magic creates an iterator that returns one value at a time
using readline(), and stops when readline() returns ''.

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

If you're going to want to do lots of processing with the data (say
accessing Flowers after you work with Fruits) then I'd suggest massaging
the entire file into a dictionary, but that's probably overkill for what 
you're trying to accomplish.

You may also want to use .strip() and possibly .lcase() on the input
strings and compare them against 'fruit:' in order to handle slightly
malformed data, but that may not be necessary if you know your data will
be consistent.

More information about the Python-list mailing list