Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Looking for similar words in 2 .txt files

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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.

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading