Question

Python problem: 1. Create a new Jupyter Notebook, using your name for the file name as...

Python problem:

1. Create a new Jupyter Notebook, using your name for the file name as we normally do. Make sure you can download it and it is a .ipynb file and that it will work when I upload it to my Jupyter.

2. Write code that opens and reads Websites.txt and uses it to build a dictionary that contains arrays of the URL's in the file as values and keywords as keys. It should look something like the below:{'soccer':['goal.com','mls.com','espn.com'...

3. In the dictionary, keys should be unique (i.e., in the above example, 'soccer' should only appear once as a key). URL's may appear more than once (e.g., ESPN should appear in the value for 'soccer' as well as 'sports').

4. Prompt the user for a key term and print the URL's that correspond to that term. If I input 'recipes' then foodnetwork.com and recipes.com should appear.

5. If the user enters a key term that is not found in the file, just print 'No results found for your key word.'6. I will test this by running it using a text file with a completely different list of keywords and URL's.

Websites.txt:

goal.com
soccer
foodnetwork.com
food, entertainment, recipes
mls.com
soccer
espn.com
sports, soccer, basketball, football
cnn.com
news, politics
hbo.com
movies, entertainment
foxnews.com
news, politics
foxsports.com
sports, soccer, basketball, football
cbsnews.com
news, politics
recipes.com
food, recipes
fox.com
entertainment, sports
cbs.com
sports, entertainment
netflix.com
movies, entertainment
0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here's the code snippet along with output screenshot and code screenshot for proper understanding.

Just change the 'filepath' to point it to the 'websites.txt' file

If this helps you, please upvote this answer. I will appreciate that :)

Code snippet

def main():

    filepath = 'C:\\Users\\JohnDoe\\Desktop\\websites.txt'

    websites = {}

    with open(filepath) as fp:

        website = fp.readline()

       

        while website:

            categories = fp.readline()

            website = website.strip() # remove whitespaces from both side

            categories = categories.strip().replace(' ', '') # remove whitespaces and spaces

            cat_list = categories.split(',') # split categories string into list of individual category

            # for every category in the list

            # check if category exist in the dictionay

            for cat in cat_list:

                # if category doesn't exist, create a new key, value pair

                if websites.get(cat) == None:

                    websites[cat] = [website]

               # else append website to corresponding website list of the category

                else:

                    websites[cat].append(website)

            website = fp.readline()

   

    while True:

        inp = input('Enter category keyword: ')

        if websites.get(inp) == None:

            print('No results found for your keyword')

        else:

            print(inp + ':')

            for website in websites[inp]:

                print('\t' + website)

        print()

if(__name__ == '__main__'):

    main()

   

Updated Python dictionary

{'soccer': ['goal.com', 'mls.com', 'espn.com', 'foxsports.com'], 'food': ['foodnetwork.com', 'recipes.com'], 'entertainment': ['foodnetwork.com', 'hbo.com', 'fox.com', 'cbs.com', 'netflix.com'], 'recipes': ['foodnetwork.com', 'recipes.com'], 'sports': ['espn.com', 'foxsports.com', 'fox.com', 'cbs.com'], 'basketball': ['espn.com', 'foxsports.com'], 'football': ['espn.com', 'foxsports.com'], 'news': ['cnn.com', 'foxnews.com', 'cbsnews.com'], 'politics': ['cnn.com', 'foxnews.com', 'cbsnews.com'], 'movies': ['hbo.com', 'netflix.com']}

Output screenshot

Code screenshot

Add a comment
Know the answer?
Add Answer to:
Python problem: 1. Create a new Jupyter Notebook, using your name for the file name as...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT