1

Apologies is this is against the rules. I have tried creating a simple Python script that searches a text file for any of the strings in a list.

KeyWord =['word', 'word1', 'word3']

if x in Keyword in open('Textfile.txt').read():
    print('True')

When i'm running the code I am getting a "name error: name 'x' is not defined" although i'm not sure why?

1
  • If all three keywords are present in the document, how many times should "True" be printed? Commented Feb 28, 2018 at 15:46

2 Answers 2

2

You could do this with a for loop as below. The issue with your code is it does not know what x is. You can define it inside of the loop to make x equal to a value in the KeyWord list for each run of the loop.

KeyWord =['word', 'word1', 'word3']
with open('Textfile.txt', 'r') as f:
    read_data = f.read()
for x in KeyWord:
    if x in read_data:
        print('True')
Sign up to request clarification or add additional context in comments.

5 Comments

Thankyou for the support guys, I had a look over the code again and realised that I had not defined x ' KeyWord =['word', 'word1', 'word3'] for x in KeyWord: if x in open('Textfile.txt').read(): print('True') '
Is this the solution you were looking for @LockTheTaskBar or do you only want it to print True one time if any of the words are in the file? Currently it will print True for each word that is in the list that is also in the file.
Thanks @Zack Tarr, this is what I was looking to achieve. I am going to try implement a way of storing the results in a seperate text file. Also going to look at storing the word from the list that appears, rather than a true or false.
This should work fine for you then. You can use x in the if x in read_data segment to append the word to a new list of "found words" Good luck!
Thanks @Zack Tarr, will try this method, appreciate the support.
1

x is not defined. You forgot the loop that would define it. This will create a generator so you will need to consume it with any:

KeyWord =['word', 'word1', 'word3']

if any(x in open('Textfile.txt').read() for x in KeyWord):
    print('True')

This works but it will open and read the file multiple times so you may want

KeyWord = ['word', 'word1', 'word3']

file_content = open('test.txt').read()

if any(x in file_content for x in KeyWord):
    print('True')

This also works, but you should prefer to use with:

KeyWord = ['word', 'word1', 'word3']

with open('test.txt') as f:
    file_content = f.read()

if any(x in file_content for x in KeyWord):
    print('True')

All of the above solutions will stop as soon as the first word in the list is found in the file. If this is not desireable then

KeyWord = ['word', 'word1', 'word3']

with open('test.txt') as f:
    file_content = f.read()

for x in KeyWord:
    if x in file_content:
        print('True')

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.