Comparing data from 2 text files in python
Question:
There are 2 text files, the first contains words (file1.txt) that will need to be searched in the lines of another file, for example: window factory scientist
In another text file, data (file2.txt) where to look for, for example: window – Anton, game crossroads, snow factory – fairy tale, song status, scientist ring, horse
Need to find the lines that contain the words and write out the word that was found
import re
file1 = open('position.txt')
file2 = open('input.txt')
for a in file2:
for b in file1:
result = re.findall(b, a)
print(result)
``
Expected Result:
window
factory
scientist
-
Answers:
Your code appears to be on the correct path. But, you must reset the position of file1 after each line in file2 has been processed; otherwise, the second pass over file2 will fail to locate any matches. This revised version of your code should produce the intended results:
import re
file1 = open('file1.txt')
file2 = open('file2.txt')
for line2 in file2:
for word1 in file1:
word1 = word1.strip()
match = re.search(word1, line2)
if match:
print(word1)
file1.seek(0) # reset the position of file1 to the beginning
This code reads each line of file2, loops over the words in file1, searches for each word in the line using re.search(), and outputs the found word. After processing all words in file1 for a line in file2, file1 is reset to the beginning using file1. seek(0).
The revised code should generate the following output if file1.txt and file2.txt are in the same directory as your Python script and have the contents you described:
**window**
factory
scientist
If this solution works please click on the right mark left side of the answer.
There are 2 text files, the first contains words (file1.txt) that will need to be searched in the lines of another file, for example: window factory scientist
In another text file, data (file2.txt) where to look for, for example: window – Anton, game crossroads, snow factory – fairy tale, song status, scientist ring, horse
Need to find the lines that contain the words and write out the word that was found
import re
file1 = open('position.txt')
file2 = open('input.txt')
for a in file2:
for b in file1:
result = re.findall(b, a)
print(result)
``
Expected Result:
window
factory
scientist
-
Your code appears to be on the correct path. But, you must reset the position of file1 after each line in file2 has been processed; otherwise, the second pass over file2 will fail to locate any matches. This revised version of your code should produce the intended results:
import re
file1 = open('file1.txt')
file2 = open('file2.txt')
for line2 in file2:
for word1 in file1:
word1 = word1.strip()
match = re.search(word1, line2)
if match:
print(word1)
file1.seek(0) # reset the position of file1 to the beginning
This code reads each line of file2, loops over the words in file1, searches for each word in the line using re.search(), and outputs the found word. After processing all words in file1 for a line in file2, file1 is reset to the beginning using file1. seek(0).
The revised code should generate the following output if file1.txt and file2.txt are in the same directory as your Python script and have the contents you described:
**window**
factory
scientist
If this solution works please click on the right mark left side of the answer.