Python Forum
[difflib] read files with SequenceMatcher - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: [difflib] read files with SequenceMatcher (/thread-4899.html)



[difflib] read files with SequenceMatcher - JamieVanCadsand - Sep-12-2017

Hey Python Programmers...

I try to write an script to read files with difflib writting an script like this:

import difflib

file1 = open("File1.txt", "r")
file2 = open("File2.txt", "r")
result = difflib.SequenceMatcher(None, file1.read(), file2.read())

print(result)
If i run my script, i get an error. This script don't work. I don't
know what the error is, but simply said... this script don't work.

What is wrong with my script. I write this script only to learn handle
with these functions or reather... i am in the learn fase of python.
Its only to learn use the difflib functions for training.

I do this to train my self in python programming.

Can anyone help me with my script, but whats wrong with this...
Thanks for helping, Jamie van Cadsand.


RE: [difflib] read files with SequenceMatcher - wavic - Sep-12-2017

difflib.SequenceMatcher returns a difflib.SequenceMatcher object. You can't just print it. Instead, see which methods of this object are useful for your needs.

In [4]: m = difflib.SequenceMatcher(None, s1, s2)

In [5]: type(m)
Out[5]: difflib.SequenceMatcher
See the docs


RE: [difflib] read files with SequenceMatcher - buran - Sep-12-2017

what you get is not an error, but you just print the representation of
SequenceMatcher object, e.g. something like
Output:
<difflib.SequenceMatcher instance at 0x030917B0>
you need to use some of the SequenceMatcher methods to get meaningful result, e.g.
if file1.txt is

Output:
abcd
and file2.txt is

Output:
bla abcd
then

import difflib

with open("file1.txt", "r") as f1, open("file2.txt", "r") as f2:
    result = difflib.SequenceMatcher(None, f1.read(), f2.read())

print(result.get_matching_blocks())
will get you this:

Output:
[Match(a=0, b=4, size=4), Match(a=4, b=8, size=0)]
still not very human, so you may want to use some of the other classes that build on top of differ.SequenceMatcher, e.g. difflib.Differ

working with the same txt files:
import difflib

with open("file1.txt", "r") as f1, open("file2.txt", "r") as f2:
    dif = difflib.Differ()
    print('\n'.join(dif.compare(f1.read(), f2.read())))
Output:
+ b + l + a + a b c d



RE: [difflib] read files with SequenceMatcher - JamieVanCadsand - Sep-15-2017

(Sep-12-2017, 11:23 AM)buran Wrote: what you get is not an error, but you just print the representation of
SequenceMatcher object, e.g. something like
Output:
<difflib.SequenceMatcher instance at 0x030917B0>
you need to use some of the SequenceMatcher methods to get meaningful result, e.g.
if file1.txt is

Output:
abcd
and file2.txt is

Output:
bla abcd
then

import difflib

with open("file1.txt", "r") as f1, open("file2.txt", "r") as f2:
    result = difflib.SequenceMatcher(None, f1.read(), f2.read())

print(result.get_matching_blocks())
will get you this:

Output:
[Match(a=0, b=4, size=4), Match(a=4, b=8, size=0)]
still not very human, so you may want to use some of the other classes that build on top of differ.SequenceMatcher, e.g. difflib.Differ

working with the same txt files:
import difflib

with open("file1.txt", "r") as f1, open("file2.txt", "r") as f2:
    dif = difflib.Differ()
    print('\n'.join(dif.compare(f1.read(), f2.read())))
Output:
+ b + l + a + a b c d

OK, Thanks for you help with my code... maby i try your way tomorrow and thad expirimenting
with difflib at the hope thad it works. I hope thad this get succes, but now i don't can try
your code, i am working at the GlazenPui, just maby i can even tonight or tomorrow try your
code. Now i am working...

Jamie.