Python Forum
Compare all words in input() to all words in file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Compare all words in input() to all words in file
#1
Hi everyone,

So I have revised my code and hope you guys can help me out. My intention is to compare each words from user input to words in a file and print only words not exist from user input. My code is as shown below. I apologized for not know how to insert code correctly.

sentence = input("Enter a sentence: ")

text = sentence.split()

for counter in text:
#    print (str.lower(counter))

    with open('data.txt') as file: 
        content = file.read()

    for counter in content:

        if text in content:
            continue
        if text not in content:
            print(counter)

print('These words not in list')
data.txt
Output:
apples oranges bananas kiwis
Output:
Error:
Enter a sentence: i love apples Traceback (most recent call last): File "test.py", line 13, in <module> if text in content: TypeError: 'in <string>' requires string as left operand, not list
I am not sure how to handle the error.

I only need to pull the words not exist from user input. What I want the output to print is:

Output:
i love These words not in list
Please advise.
Reply
#2
First, file.read() returns a string. When you loop through a string (as in for counter in content:), you loop through the characters of the string. You want to loop through the words in content, not the characters. So you need to use the split method as you did for sentence.

Second, as the error you got shows, you want to test for string in list, not list in string. So flip the variables around the in operator.

Third, you first if clause is not needed. If the second if clause doesn't trigger, the loop goes to the next iteration, which is all your first if clause is doing.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare current date on calendar with date format file name Fioravanti 1 209 Mar-26-2024, 08:23 AM
Last Post: Pedroski55
  find and group similar words with re? cartonics 4 717 Oct-27-2023, 05:36 PM
Last Post: deanhystad
  Function to count words in a list up to and including Sam Oldman45 15 6,534 Sep-08-2023, 01:10 PM
Last Post: Pedroski55
  Form that puts diacritics on the words in the text Melcu54 13 1,463 Aug-22-2023, 07:07 AM
Last Post: Pedroski55
  docx insert words of previuos paragraph on next paragraph in the same position ctrldan 7 1,237 Jun-20-2023, 10:26 PM
Last Post: Pedroski55
  Pulling Specifics Words/Numbers from String bigpapa 2 750 May-01-2023, 07:22 PM
Last Post: bigpapa
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,111 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  Need to match two words in a line tester_V 2 868 Nov-18-2022, 03:13 AM
Last Post: tester_V
  Output difference from 2 lists of different sizes with words gracenz 5 1,294 Sep-02-2022, 05:09 PM
Last Post: Larz60+
  python-docx regex : Browse the found words in turn from top to bottom Tmagpy 0 1,514 Jun-27-2022, 08:45 AM
Last Post: Tmagpy

Forum Jump:

User Panel Messages

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