Python Forum

Full Version: Cant use difflib with islice?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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()
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)
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))