Python Forum
Cant use difflib with islice?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Cant use difflib with islice?
#1
Sorry to be back so soon.... Any ideas why my code causes Differ to crash? (NB The 3,7 is just an arbitrary number for testing a range). The "for x in R1" added just for a test works fine.

import difflib
import itertools
difference = difflib.Differ()

file1 = "gonk1.txt"
file2 = "gonk2.txt"
result_file = "resultfile.txt"  

cnt1=0
with open(result_file, "w") as f3, open(file1, "r") as f1, open(file2, "r") as f2:

    r1 = itertools.islice(f1, 3,7)
    r2 = itertools.islice(f2, 3,7)

    for x in r1:
        print(x)

    for x in difference.compare(r1, r2): 
        print(x)
Error:
File "c:\Users\gonks\Desktop\python_work\compare_v4.py", line 18, in <module> for x in difference.compare(r1, r2): ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Program Files\Python312\Lib\difflib.py", line 859, in compare cruncher = SequenceMatcher(self.linejunk, a, b) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Program Files\Python312\Lib\difflib.py", line 182, in __init__ self.set_seqs(a, b) File "C:\Program Files\Python312\Lib\difflib.py", line 194, in set_seqs self.set_seq2(b) File "C:\Program Files\Python312\Lib\difflib.py", line 248, in set_seq2 self.__chain_b() File "C:\Program Files\Python312\Lib\difflib.py", line 296, in __chain_b n = len(b) ^^^^^^ TypeError: object of type 'itertools.islice' has no len()
Reply
#2
differ.compare method expects sequences that support len() function,which itertools.islice dos not.
To fix this, you need to convert the islice objects into a list or another sequence type that supports len().
import difflib
import itertools

difference = difflib.Differ()
file1 = "gonk1.txt"
file2 = "gonk2.txt"
result_file = "resultfile.txt"

with open(result_file, "w") as f3, open(file1, "r") as f1, open(file2, "r") as f2:
    r1 = list(itertools.islice(f1, 3, 7))
    r2 = list(itertools.islice(f2, 3, 7))
    for x in difference.compare(r1, r2):
        print(x)
        f3.write(x)
gonksoup likes this post
Reply
#3
Read the documentation. From

https://docs.python.org/3/library/diffli...er-objects

Quote:compare(a, b)
Compare two sequences of lines, and generate the delta (a sequence of lines).

Each sequence must contain individual single-line strings ending with newlines. Such sequences can be obtained from the readlines() method of file-like objects. The delta generated also consists of newline-terminated strings, ready to be printed as-is via the writelines() method of a file-like object.

So what does readlines() return?

From

https://docs.python.org/3/library/io.htm....readlines

Quote:readlines(hint=-1, /)ΒΆ
Read and return a list of lines from the stream. hint can be specified to control the number of lines read: no more lines will be read if the total size (in bytes/characters) of all lines so far exceeds hint.
So Differ().compare(a, b) needs a and b to lists. itertools.islice() makes an iterator, but not a list. You would need to do something like this:
    r1 = list(itertools.islice(f1, 3, 7))
    r2 = list(itertools.islice(f2, 3, 7))
gonksoup likes this post
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Need help with itertools.islice() Charles1 2 2,896 Sep-19-2018, 10:32 AM
Last Post: Charles1
  [difflib] read files with SequenceMatcher JamieVanCadsand 3 4,805 Sep-15-2017, 09:15 AM
Last Post: JamieVanCadsand
  get only additions in difflib.unified_diff metulburr 3 5,457 May-08-2017, 08:12 AM
Last Post: volcano63

Forum Jump:

User Panel Messages

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