Home > Mobile >  Creating a search function in a list from a text file
Creating a search function in a list from a text file

Time:03-23

everyone. I have a Python assignment that requires me to do the following:

  1. Download this CSV fileLinks to an external site of female Oscar winners (https://docs.google.com/document/d/1Bq2T4m7FhWVXEJlD_UGti0zrIaoRCxDfRBVPOZq89bI/edit?usp=sharing) and open it into a text editor on your computer
  2. Add a text file to your sandbox project named OscarWinnersFemales.txt
  3. Copy and paste several lines from the original file into your sandbox file. Make sure that you include the header.
  4. Write a Python program that does the following:
  • Open the file and store the file object in a variable
  • Read the entire contents line by line into a list and strip away the newline character at the end of each line
  • Using list slicing, print lines 4 through 7 of your file
  • Write code that will ask the user for an actress name and then search the list to see if it is in there. If it is it will display the record and if it is not it will display Sorry not found.
  • Close the file

Below is the code I currently have. I've already completed the first three bullet points but I can't figure out how to implement a search function into the list. Could anyone help clarify it for me? Thanks.

f = open('OscarsWinnersFemales.txt')
f = ([x.strip("\n") for x in f.readlines()])
print(f[3:7])

Here's what I tried already but it just keeps returning failure:

def search_func():
    actress = input("Enter an actress name: ")
    for x in f:    
        if actress in f:
            print("success")
        else:
            print("failure")

search_func()

CodePudding user response:

I hate it when people use complicated commands like ([x.strip("\n") for x in f.readlines()]) so ill just use multiple lines but you can do what you like.

f = open("OscarWinnersFemales.txt")
f = f.readlines()
f.close()

data = {}  # will list the actors and the data as their values

for i, d in enumerate(data):
    f[i] = d.strip("\n")
    try:
        index, year, age, name, movie = d.split(",")
    except ValueError:
        index, year, age, name, movie, movie2 = d.split(",")
        movie  = " and "   movie2
    data[name] = f"{index}-> {year}-{age} | {movie}"

print(f[3:7])

def search_actr(name):
    if name in data: print(data[name])
    else: print("Actress does not exist in database. Remember to use captols and their full name")

I apologize if there are any errors, I decided not to download the file but everything I wrote is based off my knowledge and testing.

CodePudding user response:

I have figured it out

file = open("OscarWinnersFemales.txt","r")

OscarWinnersFemales_List = []

for line in file: stripped_line = line.strip() OscarWinnersFemales_List.append(stripped_line)

print(OscarWinnersFemales_List[3:7]) print()

actress_line = 0 name = input("Enter An Actress's Name: ") for line in OscarWinnersFemales_List: if name in line: actress_line = line break if actress_line == 0: print("Sorry, not found.") else: print() print(actress_line)

  • Related