D
D
DAnya782021-07-28 14:05:23
Python
DAnya78, 2021-07-28 14:05:23

String comparisons from array and file?

I need to output all non-matching lines from the file

, here is the code, but I don’t understand what the error is

a = ['1','2'] 
file = open('test.txt')
for line in file:
  if line not in a:
    print(line)

file lines like
1
2
3
4
5
need to get 3,4,5

Answer the question

In order to leave comments, you need to log in

1 answer(s)
U
UberPool, 2021-07-28
@Danya78

It seems to me that you have not read the data from the file, after opening it, you need to read the data.
And then compare them with the list.

file = open('test.txt')
data = file.read()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question