Opening random line from file with random.shuffle() 
Author Message
 Opening random line from file with random.shuffle()

Hello-

        I need to get a random line from a file. I read in thins NG about a "poorly" documented random.shuffle(), but I can't get it to work:

#!/usr/local/bin/python2.0

import random
file = open('/xxxx/yyy/zzz.txt')

allLines = file.readlines()

file.close()

getLine = random.shuffle(allLines)

print getLine,
string = str(getLine)

print string,

        All I get as output is
        "None None"

        Thanks for __any__ help!

        Regs
        Henry

----------------------
_________   1001101010
| ^   ^ |  /1111100110
| o   o | / 0010100111
|   v   |/  1110100111
| ----- |      
\_______/

______________________
Micro$oft-Free Human
100% Debian GNU/Linux
----------------------
        KMFMS



Mon, 02 Aug 2004 08:52:18 GMT  
 Opening random line from file with random.shuffle()


Quote:

> Hello-

> I need to get a random line from a file. I read in thins NG about a

"poorly" documented random.shuffle(), but I can't get it to work:

Quote:

> #!/usr/local/bin/python2.0

> import random
> file = open('/xxxx/yyy/zzz.txt')

> allLines = file.readlines()

> file.close()

> getLine = random.shuffle(allLines)

Shuffles it in place and returns None.  Try,

getLine = random.choice(allLines)

Quote:
> print getLine,
> string = str(getLine)

> print string,

> All I get as output is
> "None None"

> Thanks for __any__ help!

> Regs
> Henry

> ----------------------
> _________   1001101010
> | ^   ^ |  /1111100110
> | o   o | / 0010100111
> |   v |/  1110100111
> | ----- |
> \_______/

> ______________________
> Micro$oft-Free Human
> 100% Debian GNU/Linux
> ----------------------
> KMFMS



Mon, 02 Aug 2004 10:09:42 GMT  
 Opening random line from file with random.shuffle()

Quote:

> Hello-

>       I need to get a random line from a file. I read in thins NG about a
"poorly"
> documented random.shuffle(), but I can't get it to work:

>>> f = open('/home/shaleh/apt.howto')
>>> lines = f.readlines()
>>> import random
>>> random.choice(lines)

'Apt is a suite of utilities that assist the admin in maintaining an\n'

random.choice() is really cool.  Give it any sequence (list, tuple, things that
act like a sequence, etc) and it chooses one item and returns it.  You could
ask for a random card from a list of cards, name in a list of names, etc.



Mon, 02 Aug 2004 10:10:02 GMT  
 Opening random line from file with random.shuffle()

Quote:

>  I need to get a random line from a file.  I read in thins NG about a
> "poorly" documented random.shuffle(), but I can't get it to work:

I just checked the documentation.  It seems well enough documented
to me, even indicating that it does not return anything but shuffles
the sequence in place.  Right above it is the choice() method,
which clearly behaves as you wish.  I find the comment about
poor documentation quite curious in this case.


Mon, 02 Aug 2004 12:06:06 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. random rant on random files

2. Any Implimentation of Random Select/Shuffle ?

3. random.shuffle?

4. random.shuffle question

5. Random Line from a File

6. Reading a random line from a file

7. Extracting a random line from a file in Emacs Lisp

8. A truly random $random??

9. ? generating random uniform and binomial random deviates for BIG integers

10. Unit tests and random.Random()

11. Random number not random?

12. random behavior in random module?

 

 
Powered by phpBB® Forum Software