It is not clear to me what you are really aiming for here, since your example is somewhat ambiguous.
But, it seems as though you do NOT want to compare the files word by word, because then you would get a very different output than what you describe in your example. My guess is that you want to check if a
word from file2 exists in the corresponding
line of file1.
If that is indeed the case, then this will work:
Code:
#!/usr/bin/env python3
"""
http://www.linuxquestions.org/questions/linux-software-2/find-difference-of-file-line-by-line-4175546931/
"""
# Open the files
with open("file1.txt") as f1, open("file2.txt") as f2:
# Read both files. f1 as a list with lines,
# f2 as a string
f1Lines = f1.readlines()
f2Str = f2.read()
# Split f2 into list based on WORDS
f2Split = f2Str.split()
lineNum = 1
j = 0
# Now loop
for i in range(0, len(f2Split)):
# Compare WORD of file2 against WORDS in file1,
# line by line
if f2Split[i] not in f1Lines[j]:
print("Line " + str(lineNum) + ": " + f2Split[i])
# If you're at the 3rd word in f2, simulate a
# new 'line' and add 1 to index j
if (i+1)%3 == 0:
j+=1
lineNum+=1
If I run this code on your example files, I get the following output:
Code:
./pydiff.py
Line 1: Cook
Line 2: Giraffe
But then there's the odd Cat in your example:
Quote:
Such thatg if i do a diff on line 1 of both files i should get
Cook
and in line2 i should get
Cat Giraffe
|
I really don't understand why you should get 'Cat' as output. If you follow the logic of MY program, it looks for each word in file2, if it is NOT in the same line in file1 it prints it.
So...
file2, line2: Giraffe Camel Dog
file1, line2: Cat Dog Camel
There is no 'Giraffe' in line 2 of file1, therefore it prints.
But by YOUR logic, you should not only get:
Quote:
and in line2 i should get
Cat Giraffe
|
But you should ALSO get the following output from line1:
Cook Harry
Why? because there are no 'Harry's in line1 of file2.
So... if you want to do THAT kind of a check, well, it can be implemented with a couple of more lines of code, but as I said above, it is far from clear to me what it is you actually want to achieve here.
Best regards