Posts: 33
Threads: 19
Joined: Aug 2017
Hey Python Programmers...
I try to write an script to read files with difflib writting an script like this:
1 2 3 4 5 6 7 |
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.
Posts: 2,953
Threads: 48
Joined: Sep 2016
Sep-12-2017, 11:15 AM
(This post was last modified: Sep-12-2017, 11:15 AM by wavic.)
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.
1 2 3 4 |
In [ 4 ]: m = difflib.SequenceMatcher( None , s1, s2)
In [ 5 ]: type (m)
Out[ 5 ]: difflib.SequenceMatcher
|
See the docs
Posts: 8,169
Threads: 160
Joined: Sep 2016
Sep-12-2017, 11:23 AM
(This post was last modified: Sep-12-2017, 11:24 AM by buran.)
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
1 2 3 4 5 6 |
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:
1 2 3 4 5 |
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
Posts: 33
Threads: 19
Joined: Aug 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
1 2 3 4 5 6 |
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:
1 2 3 4 5 |
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.
|