[Tutor] reading random line from a file
Andreas Kostyrka
andreas at kostyrka.org
Sun Jul 15 08:41:40 CEST 2007
Well he could implement the indexing into his program and check mtimes to decide if he needs to reindex.
But yes, as long the file fits into memory, readlines (or list(file("quotes,txt")) makes more sense.
Andreas
-- Ursprüngl. Mitteil. --
Betreff: Re: [Tutor] reading random line from a file
Von: "Alan Gauld" <alan.gauld at btinternet.com>
Datum: 15.07.2007 06:39
"max baseman" <dos.fool at gmail.com> wrote
> im writing a quick quote reader that spits out a random quote from a
> show but cant get it to pick randomly
You can either get theclines to be the same length and
use a random index to seek() to the start of the line you want.
Or you can build a separate index file which records where
each line starts and randomly select one of thiose. But that
requires that the quotes file is only changed programmatically
so that the index file can be rebuilt each time you add/delete
a quote. (Or you build an indexing program)
Its much easier (unless the file is huge) to just use readlines()
HTH
Alan G.
_______________________________________________
Tutor maillist - Tutor at python.org
http://mail.python.org/mailman/listinfo/tutor
More information about the Tutor
mailing list