I want to find strings listed in list.txt (one string per line) in another text file in case I found it print ‘string,one_sentence’ in case didn’t find ‘string,another_sentence’. I’m using following code, but it is finding only last string in the strings list from file list.txt. Cannot understand what could be the reason?
data = open('c:/tmp/textfile.TXT').read()
for x in open('c:/tmp/list.txt').readlines():
if x in data:
print(x,',one_sentence')
else:
print(x,',another_sentence')
When you read a file with
readlines(), the resulting list elements do have a trailing newline characters. Likely, these are the reason why you have less matches than you expected.Instead of writing
write
This removes leading and trailing whitespace from the strings in
list. Hence, it removes trailing newline characters from the strings.In order to consolidate your program, you could do something like this:
I did not want to make too drastic changes. The most important difference is that I am using the context manager here via the
withstatement. It ensures proper file handling (mainly closing) for you. Also, the ‘needle’ lines are stripped on the fly using a generator expression. The above approach reads and processes the needle file line by line instead of loading the whole file into memory at once. Of course, this only makes a difference for large files.