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


Messages In This Thread
Open and read multiple text files and match words - by kozaizsvemira - Sep-11-2019, 11:15 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
Question Right way to open files with different encodings? Winfried 2 168 Apr-23-2024, 05:50 PM
Last Post: snippsat
  Open files in an existing window instead of new Kostov 2 301 Apr-13-2024, 07:22 AM
Last Post: Kostov
  Using a script to open multiple shells? SuchUmami 9 501 Apr-01-2024, 10:04 AM
Last Post: Gribouillis
  python convert multiple files to multiple lists MCL169 6 1,557 Nov-25-2023, 05:31 AM
Last Post: Iqratech
  Move Files based on partial Match mohamedsalih12 2 822 Sep-20-2023, 07:38 PM
Last Post: snippsat
  open python files in other drive akbarza 1 692 Aug-24-2023, 01:23 PM
Last Post: deanhystad
  Form that puts diacritics on the words in the text Melcu54 13 1,480 Aug-22-2023, 07:07 AM
Last Post: Pedroski55
  splitting file into multiple files by searching for string AlphaInc 2 901 Jul-01-2023, 10:35 PM
Last Post: Pedroski55
  Start print a text after open an async task via button Nietzsche 0 707 May-15-2023, 06:52 AM
Last Post: Nietzsche
  Merging multiple csv files with same X,Y,Z in each Auz_Pete 3 1,173 Feb-21-2023, 04:21 AM
Last Post: Auz_Pete

Forum Jump:

User Panel Messages

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