for loop - Python Text Document Translation Comparison -
fairly simple question. i'm trying create "translation comparison" program reads , compares 2 documents , returns every word isn't in other document. beginner class, i'm trying avoid using obscure internal methods, if means less efficient code. have far...
def translation_comparison(): import re file1 = open("desktop/file1.txt","r") file2 = open("desktop/file2.txt","r") text1 = file1.read() text2 = file2.read() text1 = re.findall(r'\w+',text1) text2 = re.findall(r'\w+',text2) item in text2: if item not in text1: return item
you might try
#######test data #file1.txt = test #file2.txt = test #results# #is def translation_comparison(): open("file1.txt", 'r') f1: f1 = f1.read().split() open("file2.txt", 'r') f2: f2 = f2.read().split() word in f1: if word not in f2: print(word) translation_comparison()
also practice use
with open("file1.txt", 'r') f1: f1 =f1.read().split()
because when using open files close file when you're not using it. python pretty @ releasing , managing memory habit make sure release or call
file1.close()
when done.
Comments
Post a Comment