[Tutor] Fastest way to iterate through a file
Kent Johnson
kent37 at tds.net
Tue Jun 26 16:04:07 CEST 2007
Robert Hicks wrote:
> This is the loop code:
>
> for line in f2:
> for id in idList:
> if id in line:
> print "%s: %s" % (id, f2.next())
> found = "%s: %s" % (id, f2.next())
> f3.write(found)
>
>
> I have an list, idList[], that contains a list of id numbers. That code
> will loop the the f2 file and for lines that have an id on it it will
> print the "next" line (so I can see what it is doing) and write it to a
> file. I will turn off that screen print after I get it going the way I
> want it to.
I don't see any particular reason this should be slow unless idList is
large. Perhaps the output is being buffered somewhere and not appearing
until the process is done? How are you running the program?
BTW the line that you print and the line that you save to the file are
not the same; every time you call f2.next() it will fetch another line
from the file. If you want them to be the same you could say
found = "%s: %s" % (id, f2.next())
print found
f3.write(found)
Kent
More information about the Tutor
mailing list