Python Forum
[difflib] read files with SequenceMatcher
Thread Rating:
  • 1 Vote(s) - 3 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[difflib] read files with SequenceMatcher
#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


Messages In This Thread
RE: [difflib] read files with SequenceMatcher - by JamieVanCadsand - Sep-15-2017, 09:15 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Cant use difflib with islice? gonksoup 2 431 Jan-22-2024, 01:07 PM
Last Post: deanhystad
  How to read in mulitple files efficiently garynewport 3 965 Jan-27-2023, 10:44 AM
Last Post: DeaD_EyE
  Read directory listing of files and parse out the highest number? cubangt 5 2,538 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,473 Feb-13-2022, 07:13 PM
Last Post: menator01
  Open and read multiple text files and match words kozaizsvemira 3 6,861 Jul-07-2021, 11:27 AM
Last Post: Larz60+
  matching with SequenceMatcher ratio two dataframe zoropython 2 3,105 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,919 Dec-06-2020, 04:14 PM
Last Post: Divya577
  How to read csv files parallay Mekala 2 2,075 Oct-24-2020, 07:33 AM
Last Post: Mekala
  Read KML files, edit items, and rewrite files? Winfried 4 4,896 Aug-21-2020, 03:55 PM
Last Post: Winfried
  Python: Automated Script to Read Multiple Files in Respective Matrices Robotguy 7 4,359 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