Python newbie Python newbie - 29 days ago 12
Python Question

PYTHON How to check if a line contains specific letters and if not print the line

I have a text file with lots of data in.
I am trying to read the file line by line, and check if any of the letters in each line are letters in the word "hello".

Then I would like to print any line that does not contain either an h,e,l,l,o

My text file is called data.txt

Here is more code so far:

hello = list('hello')
with open('data.txt', 'r') as file.readlines:
for line in file:
if hello not in line:
print(line)


but currently line 3 produces the error; NameError: name 'file' is not defined

update:

hello = list('hello')
with open('data.txt', 'r') as f:
for line in f:
s = set(line)
if all(i not in s for i in hello):
print(line)


Thank you for the help, now lots of lines of the text file have been eliminated, however "Epping" still prints, which has an "e" in it, as does the word "hello" and therefore it should be excluded?

Answer

You're opening your file wrong.

hello = list('hello')
with open('data.txt', 'r') as f:
    for line in f:
        s = set(line)
        if all(letter not in s for letter in hello):
            print(line)