Home > Back-end >  Print out a random string between certain characters from a text file
Print out a random string between certain characters from a text file

Time:04-24

Hi I have a text file named "fortunes" with a lot of data containing many differens quotes. Each quote is separated with the character "%". What I am struggling to do is to make a code that will print out a random quote out the whole text file, meaning that the random print should contain a string that is between the separator(%). Does anyone have any suggestions of an easy fix to this? As you can see I have started on something, but I am not sure where to include to random. function in all of this. Thank you so much in advance

enter image description here

CodePudding user response:

Split the file into a list around % and then just pick a random list element:

with open('fortunes.txt') as file:
  data = file.read()

quote = random.choice(data.split('%'))
print(quote)

CodePudding user response:

I tried this:

import random

with open('./quotes.txt') as file:
    quote = random.choice(file.read().split("%"))

print(quote)

With a text file called quotes.txt that contains this:

Random quote
- HeyHoo
%
Foo is a great bar to start you off in the morning
- HeyHoo
%
Try sham-foo today! It'll make turn your hair into a bar of gold!
- ShamFoo™️

And it worked pretty well. You should look into the split() function & read the documentation.

CodePudding user response:

Assume "in.txt" is

%
A very deep quote.
  -- a very deep person.
%
An even deeper quote.
  -- an even deeper person.
%
An even deeper quote!
  -- an even deeper person!
%

Notice how it begins and terminates with a %. This program will randomly choose between one of A very deep quote., An even deeper quote., and An even deeper quote! and print it out (along with the author):

import random

with open("in.txt") as f:
    quotes = f.read().split('%')

# if file does not begin with nor end with '%' can exclude
# both of these "if not ..."
if not quotes[0].strip():
    del quotes[0]
if not quotes[-1].strip():
    del quotes[-1]

random_quote = random.choice(quotes).strip()
print(random_quote)

The program

  • opens "in.txt" for reading.
  • f.read() returns a string of all the text in "in.txt" (only call the function without arguments if "in.txt" is not too large -- by default, the sole argument of read is size = -1 which means to return all the characters in the file).
  • split('%') returns a list of the "words" in the string, using '%' as the delimiter. Since the optional second argument of split() is not specified, all possible splits are made.
  • As we exit the with block, internally, the file is closed automatically for us.
  • If the first or last element of quotes, stripped of leading and trailing whitespace, is the empty string, we delete it from the list. We do this to allow the file to begin or end with a '%'.
  • random.choice(quotes) returns a randomly selected element from quotes (which will be a string).
  • strip() removes leading and trailing whitespace from this string.

Output of the program will be one of

An even deeper quote!
  -- an even deeper person!

or

An even deeper quote.
  -- an even deeper person.

or

A very deep quote.
  -- a very deep person.
  • Related