Trying to find if an element in one file exists in another file

1.1k Views Asked by At

Im still new to python so ill try my best to explaining what I have done So basically my code asks the user for input it then imports a text file which contains words and each word is in a separate line, my code then store the users input in a separate file and only uses the first line of the user input I am trying to see if the words that my user input exists in the pre set text file I imported and then in my full code I would do some operations on that words but its not working i have tried using "==" and using "counter" and "if x in y...." and also if the user input does exist in text file the count on the last line prints zero.

user_input=input("Enter: ")
user_input=user_input.upper()
user_input=user_input.split()
print(user_input)
list_words=[]
with open("words.txt","r") as words:
    english_words=words.readlines()
    for line in english_words:
       line=line.upper()
       list_words.append(line.rstrip('\n'))
with open("user-message.txt","w+") as file_of_user_inp:
       for word in user_input:
           file_of_user_inp.write(word +"\n")
           
with open("user-message.txt","r") as file_of_user_inp:
       first_line_user_inp=file_of_user_inp.readline()
       user_input=first_line_user_inp

for each_eng_word in list_words:
    for each_word in user_input:
        if each_word==each_eng_word:
            print("it worked")

print(list_words.count((user_input)))
1

There are 1 best solutions below

0
On

Here's a program I made that might be able to help. You can type a list of words separated by spaces and it will check a text document for any matches. If it finds a match it will print the matching word(s).

The First thing I did was create variables the user_input, list1 which holds all of the words from your text document, numbers, WordString which is a temporary string that holds your words before they are added to the list.

user_input = input("Enter: ")
list1 = []
numbers = "1234567890"
WordString = ""

Here I open a text document and use the read() function to read the text document.

with open("YOUR TEXT DOCUMENT", "r") as b:
    words = b.read()

Here I loop through every character inside your text document using a "for loop", "range()", and "len()".

The first "if conditional" checks if the xth letter in the text document is not a space and checks if the xth letter is not a number (or not in our "number variable" which holds a string of numbers anyway.) This allows all consecutive letters to be counted as words. We add these letters in order to the "WordString" string.

The second "if conditional" checks if the word has ended or not. It does this by checking if you have reached a space or a number or the end of the text document. If it has met any of these conditions it will add your finished word to the list1 array. The "-1" is because len() measures characters starting at one and items inside arrays start counting at 0. Once we find a complete word we reset WordString back to a empty string so it is ready to go through the loop again.

for x in range(len(words)):
    if words[x] != " " and words[x] not in numbers:
        WordString += words[x]
    if words[x] == " " or words[x] in numbers or x == len(words) - 1:
        list1.append(WordString)
        WordString = ""

This second loop also checks for words, but it's looking for the words that you typed in the "user_input" variable instead. It also reuses the "WordString" variable from before.

The only difference lies in the second if statement. Once we find a complete, finished word. We check to see if it is a word that is inside of the list1 array (the place where we put all the words from the text document.). If if it is infact a word inside of the list1 array we print it.

for y in range(len(user_input)):
    if user_input[y] != " " and user_input[y] not in numbers:
        WordString += user_input[y]
    if user_input[y] == " " or user_input[y] in numbers or y == len(user_input) - 1:
        if WordString in list1:
            print(WordString)
        WordString = ""

I tried to explain it best I could, I'm a beginner with Python also. There may be a more efficient way and I'm sure other veterans can explain it better. But here's my input (no pun intended).