Python Forum
Open and read multiple text files and match words
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Open and read multiple text files and match words
#1
Hi,

How can I create a script that reads two text files and prints out words that match within text file number 1?
This code below is the furthest I got, it can match words in string and print it out, but I need it to read two or more large text files and print same found matched words. Thank you.

import re

def get_words_from_string(s):
    return set(re.findall(re.compile('\w+'), s.lower()))

def get_words_from_file(fname):
    with open(fname, 'rb') as inf:
        return get_words_from_string(inf.read())

def all_words(needle, haystack):
    return set(needle).issubset(set(haystack))

def any_words(needle, haystack):
    return set(needle).intersection(set(haystack))

search_words = get_words_from_string("this my test")
find_in = get_words_from_string("If this were my test, I is passing")

print (search_words)
Reply
#2
This can be compacted by using list comprehension, but will do the job
import os

def get_words(filename):
    wordlist = []
    with open(filename) as fp:
        for line in fp:
            wordsinline = line.strip().split()
            for item in wordsinline:
                if item not in wordlist:
                    wordlist.append(item)
    return wordlist

def find_common_words(filename1, filename2):
    wordlist1 = []
    wordlist2 = []
    matching_words = []

    wordlist1 = get_words(filename1)
    wordlist2 = get_words(filename2)

    matching_words = set(wordlist1) & set(wordlist2)
    print(matching_words)

def testit():
    # Assert in same directory as code
    os.chdir(os.path.abspath(os.path.dirname(__file__)))
    filename1 = 'words1.txt'
    filename2 = 'words2.txt'
    find_common_words(filename1, filename2)

if __name__ == '__main__':
    testit()
Reply
#3
Thank you my man, love you.
Reply
#4
I see my varbatim response on stackoverflow https://stackoverflow.com/questions/tagged/python how nice.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  python convert multiple files to multiple lists MCL169 6 1,403 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Move Files based on partial Match mohamedsalih12 2 713 Sep-20-2023, 07:38 PM
Last Post: snippsat
  open python files in other drive akbarza 1 614 Aug-24-2023, 01:23 PM
Last Post: deanhystad
  Form that puts diacritics on the words in the text Melcu54 13 1,346 Aug-22-2023, 07:07 AM
Last Post: Pedroski55
  splitting file into multiple files by searching for string AlphaInc 2 780 Jul-01-2023, 10:35 PM
Last Post: Pedroski55
  Start print a text after open an async task via button Nietzsche 0 656 May-15-2023, 06:52 AM
Last Post: Nietzsche
  Merging multiple csv files with same X,Y,Z in each Auz_Pete 3 1,063 Feb-21-2023, 04:21 AM
Last Post: Auz_Pete
  Read text file, modify it then write back Pavel_47 5 1,465 Feb-18-2023, 02:49 PM
Last Post: deanhystad
  unittest generates multiple files for each of my test case, how do I change to 1 file zsousa 0 900 Feb-15-2023, 05:34 PM
Last Post: zsousa
  How to read in mulitple files efficiently garynewport 3 831 Jan-27-2023, 10:44 AM
Last Post: DeaD_EyE

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020