Question: how would I create a dictionary from a text file in python and then read through another text file using that dictionary as my guide

how would I create a dictionary from a text file in python and then read through another text file using that dictionary as my guide for which words to look for? This is what I currently have. I have it so that it takes the words from the file and turns each into its own key with a value of 1. I want to take that same dictionary and apply it to my second file so that it only reads the words given from the first dictionary, adding a value for every time one of the keywords repeats.

how would I create a dictionary from a text file in python

text = input("Please enter the name of the file containing the words to count> ") myFile = open(text) File = myFile. read() line = File.split() words = {} for each in line: if each not in words.keys(): words (each) = 0 words (each) += 1 print (words) text2 = input("Please enter the name of the file containing a story> ") myFile2 = open(text2) File2 = myFile2.read() line2 = File2.split() for each in line2: if each not in words.keys(): words (each) = 0 words [each] += 1 print(words)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!