[Tutor] read a random line from a file (fwd)
Danny Yoo
dyoo@hkn.eecs.berkeley.edu
Mon, 17 Dec 2001 12:02:11 -0800 (PST)
Hi Kirk,
Let me forward this to the other people here. Your function looks
good; there's just one line that might need a fix:
###
x = int(1+random.random()*l)
return db[x]
###
Is it possible for x to be 0? The reason this is important is because
db[0] is the first line in our file --- if we don't allow the possibility
of returning the first line in a file, it's less random than it should be.
*grin*
This is an example of a "boundary" case: a "boundary" case just means to
tread carefully around the edges of our problem. In this case, the edges
we should think about are the first and last lines of a file --- make sure
that the function can deal with them well.
Good luck!
---------- Forwarded message ----------
Date: Mon, 17 Dec 2001 14:14:04 -0500
From: Kirk Bailey <deliberatus@my995internet.com>
To: Danny Yoo <dyoo@hkn.eecs.berkeley.edu>
Subject: Re: [Tutor] read a random line from a file
This works:
def fileoneline(filename):
f1=open(filename,'r')
db=f1.readlines()
f1.close()
l=len(db)
x=int(1+random.random()*l)
return db[x]
Can it be improved?
Danny Yoo wrote:
>
> On Mon, 17 Dec 2001, Andrei Kulakov wrote:
>
> > On Sun, Dec 16, 2001 at 11:20:29PM -0500, Kirk Bailey wrote:
> > > OK, I know there is a simple way to do it, and I cannot locate the
> > > correct ommand. I want to open a flat text file, read ONE randomly
> > > selected line into a string, and close the file. I need the command for
> > > reading a line at random, and what module it is in. I understand there
> > > is such a command already defined, and it is simple to use, but canot
> > > find it again.
>
> Actually, I'm not sure if it's built in at all. However, we can cook up a
> few definitions so that it becomes a tool we can use.
>
> To get a random line from a file, it might be useful to write something
> that tells us how many lines a file contains. Here's one way to do it:
>
> ###
> def countLines(file):
> """Given a file, returns the number of lines it contains.
>
> The current file position should be preserved as long as the file
> supports tell() and seek()."""
> old_position = file.tell()
> count = 0
> while file.readline() != '':
> count = count + 1
> file.seek(old_position)
> return count
> ###
>
> If we had a way to choose a random number between 0 and the countLines()
> of a file, we'd be in business. Thankfully, there is something in the
> library that can help us: random.randrange():
>
> ###
> >>> random.randrange(10)
> 4
> >>> random.randrange(10)
> 7
> >>> random.randrange(10)
> 4
> ###
>
> There is documentation on random.randrange() here:
>
> http://www.python.org/doc/lib/module-random.html
>
> if you're interested in playing around with random stuff. *grin*
>
> Also, it would be great if, when we knew which line we wanted to pull out,
> we could get get that particular line. There's a module called
> 'linecache' that can do this for us:
>
> http://www.python.org/doc/lib/module-linecache.html
>
> But if 'linecache' wasn't there, it still wouldn't be too hard to cook
> something up like it.
>
> With these tools, it becomes possible to write a simple random line
> grabber:
>
> ###
> def getRandomLine(filename):
> """Given a filename, returns a random line."""
> linecount = countLines(open(filename))
> chosen_line_number = random.randrange(linecount)
> return linecache.getline(filename, chosen_line_number)
> ###
>
> Writing function definitions can be a lot of fun. Hope this helps!
>
> _______________________________________________
> Tutor maillist - Tutor@python.org
> http://mail.python.org/mailman/listinfo/tutor
--
Respectfully,
-Kirk D Bailey (C)2001
Addme! icq #27840081
end
Within the sweep of his sword, Each man is an Ubar.
http://www.howlermonkey.net/
http://www.sacredelectron.org/