I'm trying to compare two .txt files, where one is dictionary (full names) and another - solid text.
The task is to find out if there is full name from the dictionary in solid text and, if there is, write the last name into a variable.
Write now i'm trying just to create a new file with full name, but new file just fully copy the original solid text, and I really don't understand what is wrong.
import re
with open('names.txt') as fh:
words = set(re.split(r'[ \n\r] ', fh.read())) # set of searched words
with open('solid_text.txt') as file_in, \
open('hits.txt', 'w') as file_out:
for line in file_in:
if any(word in line for word in words): # look for any of the words
file_out.write(line)
The only thing I understand is that I probably digging to the wrong direction and this decision has no right to life.
CodePudding user response:
but new file just fully copy the original solid text
Because you are writing the lines from the input file, not any data in your words
list. If you want to write a name, you need to use that instead.
For clarity, I'd rename the words
as names
.
def get_last_name(name):
return name.split()[-1]
with open('names.txt') as fh:
names = set(line.rstrip() for line in fh)
with open('solid_text.txt') as file_in, \
open('hits.txt', 'w') as file_out:
for line in file_in:
for name in names:
if name in line:
file_out.write('{}\n'.format(get_last_name(name)))
Depending on your expected output, this will write all names that are seen in each line rather than only the first, as using any()
function would do