Python Forum
Import random from a txt file
Thread Rating:
  • 3 Vote(s) - 4 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Import random from a txt file
#1
Hey guys, I would just like to know how you import a random selection from a list that has been imported into python via a txt file as im trying to make like a dvd menu movie library thingy

I am trying to import a file from a folder then print a random title from the txt file but I get this strange output

here is my code

import random
print("***Movie Title Explorer***")
print("l – load file of movie titles"'\n'"r – random movie"'\n'"s– search"'\n'""
      "sw – starts with"'\n'"k – keep - save the last displayed movie title to your favourites"'\n'"f – favourites display"'\n'""
      "c – clear"'\n'"q - quit program")
print("command = ?")
selection=input("Please Select:")
if selection =='l':
    file = open("C:\\Users\\LiquidO\\Desktop\\Assingment 2 python\\movies.txt", "r")
    movies_list = file.readlines() # load each line into a list
    file.close() # close the file
    movies_list = [movie.strip() for movie in movies_list]
    print("Movies now loaded")
selection = input("Please Select:")
if selection =='r':
    print(random.choice([file]))
***Movie Title Explorer***
l – load file of movie titles
r – random movie
s– search
sw – starts with
k – keep - save the last displayed movie title to your favourites
f – favourites display
c – clear
q - quit program
command = ?
Please Select:l
Movies now loaded
Please Select:r
<_io.TextIOWrapper name='C:\\Users\\LiquidO\\Desktop\\Assingment 2 python\\movies.txt' mode='r' encoding='cp1252'>

Process finished with exit code 0

My bad I forgot to take out line 11 and also make line 16's string movies_list not file lol
    file.close() # close the file
Although I can now print the list it seems it still prints the whole contents of the txt file. Any idea how to print just one random item from the list rather than the whole lot?

import random
print("***Movie Title Explorer***")
print("l – load file of movie titles"'\n'"r – random movie"'\n'"s– search"'\n'""
      "sw – starts with"'\n'"k – keep - save the last displayed movie title to your favourites"'\n'"f – favourites display"'\n'""
      "c – clear"'\n'"q - quit program")
print("command = ?")
selection=input("Please Select:")
if selection =='l':
    file = open("C:\\Users\\LiquidO\\Desktop\\Assingment 2 python\\movies.txt", "r")
    movies_list = file.readlines() # load each line into a list
    movies_list = [movie.strip() for movie in movies_list]
    print("Movies now loaded")
selection = input("Please Select:")
if selection =='r':
    print(random.choice([movies_list]))
Reply
#2
Line 15 change to:
print(random.choice(movies_list))
You can delete line 10 and just do:
movies_list = [movie.strip() for movie in file]
Using with open() is the best approach,close file object.
Eg:
with open("C:/Users/LiquidO/Desktop/Assingment 2 python/movies.txt") as f:
    movies_list = [movie.strip() for movie in f]
Reply
#3
(May-05-2017, 09:56 PM)snippsat Wrote: Line 15 change to:
print(random.choice(movies_list))

That worked fine cheers dude!. now im just onto the rest of the command functions you wouldnt happen to know how one would Prompt for a string, and then search for and display all movies that contain this string, Search case-independently, and separate the movies with a blank line then If the search string is empty, exit the search and redisplay the menu? That is what I am stuck on at the moment lol
Reply
#4
You're already prompting for a string, so you know how to do that. To see if a word or phrase is in a given title, you use the in operator:

if 'aid' in 'Raiders of the Lost Ark':
     print('match')
If you want to make that case insensitive, the standard way is to use the upper method of the two strings, which makes them both upper case. To check against all of the movie titles, you would loop through them, check each one. You could print each one as you find it, or append it to a list in case you wanted the matches for later processing.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(May-05-2017, 10:12 PM)ichabod801 Wrote: You're already prompting for a string, so you know how to do that. To see if a word or phrase is in a given title, you use the in operator:

if 'aid' in 'Raiders of the Lost Ark':
     print('match')
If you want to make that case insensitive, the standard way is to use the upper method of the two strings, which makes them both upper case. To check against all of the movie titles, you would loop through them, check each one. You could print each one as you find it, or append it to a list in case you wanted the matches for later processing.

Maybe ive been staring blankly at the code for too long haha thanks. Makes sense

So if I was to do this

if selection =='s':
  search = str(input("Please enter a string to search"))
if search in ([movies_list]):
   print(movies_list.index)
What would I need to change in order for it to display the strings that contain the user input?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Frequency in first digit from csv file, NO IMPORT bryan0901 6 2,787 May-28-2020, 09:50 AM
Last Post: bryan0901
  Import Python file with a name of a module in Stantard Library tiago_code 7 3,047 Dec-11-2019, 06:08 AM
Last Post: ICanIBB
  Import a Data from a text file maxcom 1 2,394 Apr-01-2018, 07:30 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020