-1

This is probably something so simple. I'm scripting something to print random lines from a file but it prints one character at a time. Here is the code.

from twython import Twython, TwythonError
import random, time

filename = open('test.txt')
line = random.choice(filename.readlines())
filename.close()

for line in line:
print line

Any help is definitely appreciated. I'm a beginner so it honestly is probably something simple.

NMetallic
  • 3
  • 2

3 Answers3

1

The problem here is that random.choice will return a string. And in effect you are iterating over a string. What you should do is call split() after you call random.choice so you end up with a list of words instead of a string. Then your iteration will work as expected.

Also, you really should not iterate like this:

for line in line

Change your iterator:

for word in line

Also, it would be good practice to get used to using context managers when handling files. e.g.:

with open(some_file) as f:
    # do file actions here

So, your final solution would look like:

import random

with open('new_file.txt') as f:
    line = random.choice(f.readlines()).split()

for word in line:
    print(word)
idjaw
  • 25,487
  • 7
  • 64
  • 83
  • You're the best! Works like a charm. Thanks! – NMetallic Apr 01 '16 at 00:23
  • 1
    You forgot to instruct the OP to use `with` for the file instead of `open`/`close` directly. – jpmc26 Apr 01 '16 at 00:28
  • @jpmc26 didn't forget, was just trying to focus on OP immediate issue, but it's a good suggestion. Thanks. Added. – idjaw Apr 01 '16 at 00:31
  • I went ahead and made these changes like you suggested. I am trying to make it loop infinitely but it does not seem like it's working. while 1 == 1: Code time.sleep(10) but it wont loop – NMetallic Apr 01 '16 at 01:40
  • Why are you trying to make it run infinitely? Based on what you are trying to do, the corrections made, it should be all you need? – idjaw Apr 01 '16 at 01:40
  • I'm really just trying to practice what I have learned online but this just won't work. There's no real use for this, just me learning :) – NMetallic Apr 01 '16 at 01:45
  • @NMetallic Understood. But if you are wanting to use a while loop, then you need to use a break statement. You have to set a condition to break it on. Putting a condition in your while is dangerous. If you want to practice with a `while`, then do `while True`, and inside your while loop have a condition, that when met will `break`. – idjaw Apr 01 '16 at 01:46
1

random.choice will return only on element at a time, you have to use shuffle instead:

from twython import Twython, TwythonError
import random, time

filename = open('test.txt')
lines = filename.readlines()
filename.close()

random.shuffle(lines)

for line in lines:
    print line
ahmed
  • 5,430
  • 1
  • 20
  • 36
0

A couple things, first thing is readability:

for line in line
    print line #which line do you mean?

Now,

line = random.choice(filename.readlines())

Will just give you a random line in the file, it won't give you all the lines in a random order.

You can shuffle the array with a simple call

import random

filename = open('new_file.txt')

lines = filename.readlines()

random.shuffle(lines)

for line in lines:
    print line

You can also keep randomly taking items out of the array until it is empty

import random

filename = open('new_file.txt')

lines = set( filename.readlines() )

while( len(lines) != 0 ):
    choice = random.choice(list(lines))
    print(choice)
    lines.remove(choice)

This answer may be helpful: randomly selecting from array

Community
  • 1
  • 1