Python Forum
[difflib] read files with SequenceMatcher
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[difflib] read files with SequenceMatcher
#1
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.
Reply
#2
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
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
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
Reply
#4
(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.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Cant use difflib with islice? gonksoup 2 355 Jan-22-2024, 01:07 PM
Last Post: deanhystad
  How to read in mulitple files efficiently garynewport 3 881 Jan-27-2023, 10:44 AM
Last Post: DeaD_EyE
  Read directory listing of files and parse out the highest number? cubangt 5 2,339 Sep-28-2022, 10:15 PM
Last Post: Larz60+
  Python code to read second line from CSV files and create a master CSV file sh1704 1 2,397 Feb-13-2022, 07:13 PM
Last Post: menator01
  Open and read multiple text files and match words kozaizsvemira 3 6,738 Jul-07-2021, 11:27 AM
Last Post: Larz60+
  matching with SequenceMatcher ratio two dataframe zoropython 2 3,018 Feb-13-2021, 01:45 PM
Last Post: zoropython
  code to read files in folders and transfer the file name, type, date created to excel Divya577 0 1,857 Dec-06-2020, 04:14 PM
Last Post: Divya577
  How to read csv files parallay Mekala 2 1,984 Oct-24-2020, 07:33 AM
Last Post: Mekala
  Read KML files, edit items, and rewrite files? Winfried 4 4,769 Aug-21-2020, 03:55 PM
Last Post: Winfried
  Python: Automated Script to Read Multiple Files in Respective Matrices Robotguy 7 4,195 Jul-03-2020, 01:34 AM
Last Post: bowlofred

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020