Searching for Specific Word in txt files

I have two txt files I’m comparing and I’m trying to write see what words they have in common. I think I need and if statement and to split the files but I’m not sure how to tell python to pull the same words. This is what I have so far:

Unique_Dracula = ()
with open(r’Dracula.txt’,‘r’) as Dracula, open (r’great_expectations.txt’,‘r’, encoding=‘utf8’) as Great:
Dracula_word = Dracula.split()
Great_word = Great.split()
if Dracula_word in Great_word:
#Unique_Dracula( I know I need something here to tell python to grab the values and store them here)
print(Unique_Dracula)

Any tips will help! I’m super new to python so I’m still working out the basics. Thank you in advance!

One more suggestion, you might want to consider using regular expressions to create the list of words, because if you use split, it’s not going to handle the punctuation marks in any way; consider the difference of these two outputs, where the 2nd is simply matching a pattern of lower case (or apostrophe). You might need to think about hyphenated words as well somehow, but without going too deep, this very simple code is already a huge improvement over split. If you aren’t familiar with regex stuff, it is covered in the Scientific Computing Python course with Charles Severance on this site.

import re
str = “A rather… simple example: what happens to, for example, the punctuation?”
print(str.lower().split())
print(re.findall(“[a-z’]+”, str.lower()))

[‘a’, ‘rather…’, ‘simple’, ‘example:’, ‘what’, ‘happens’, ‘to,’, ‘for’, ‘example,’, ‘the’, ‘punctuation?’]
[‘a’, ‘rather’, ‘simple’, ‘example’, ‘what’, ‘happens’, ‘to’, ‘for’, ‘example’, ‘the’, ‘punctuation’]

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.