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
  Generate Multiple sql Files With csv inputs vkomarag 13 609 Aug-20-2021, 07:03 PM
Last Post: vkomarag
  Sorting and Merging text-files AlphaInc 10 509 Aug-20-2021, 05:42 PM
Last Post: snippsat
  How to open/load image .tiff files > 2 GB ? hobbyist 1 207 Aug-19-2021, 12:50 AM
Last Post: Larz60+
  Generate a string of words for multiple lists of words in txt files in order. AnicraftPlayz 2 315 Aug-11-2021, 03:45 PM
Last Post: jamesaarr
  Replace String in multiple text-files [SOLVED] AlphaInc 5 532 Aug-08-2021, 04:59 PM
Last Post: Axel_Erfurt
  Several pdf files to text mfernandes 10 860 Jul-07-2021, 11:39 PM
Last Post: Pedroski55
  Plotting Multiple files ! Helen_145 1 401 Jun-26-2021, 03:28 PM
Last Post: snippsat
  Reading Multiple text Files in pyhton Fatim 1 379 Jun-25-2021, 01:37 PM
Last Post: deanhystad
Question (solved) open multiple libre office files in libre office lucky67 5 678 May-29-2021, 04:54 PM
Last Post: lucky67
  Set Text in Open Dialog Box giddyhead 0 252 May-21-2021, 06:31 PM
Last Post: giddyhead

Forum Jump:

User Panel Messages

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