M. Manuel M. Manuel - 1 month ago 10
Python Question

Python difflib How it works on list?

I'm new to Python. I wrote a simple script which opens a file and with a function it appends some of the line to a

generator
object. Then I use this object to make a difference with another file read the same way. I got the following error:


unhashable type: 'list'.


For making the difference I'm using
difflib
.

Could you please explain why i get this error? I've seen how to use the
difflib
with
f.readlines()
but I do not get it because
f.readlines()
also returns a list.

#! /usr/bin/python

import difflib

def lineExtractor(file):
lines = []
for line in file:
if line.startswith('g'):
if lines:
yield lines
lines = []
else:
lines.append( line )
if lines:
yield lines

with open('testfile1.txt') as file1:
lines1 = lineExtractor(file1)
with open('testfile2.txt') as file2:
lines2 = lineExtractor(file2)
for line in difflib.unified_diff(lines1, lines2, fromfile='file1', tofile='file2', lineterm='', n=0):
print line


Thanks

Answer Source

The problem is that unified_diff needs lists of strings as input. You're giving it a generator instead. And this generator doesn't even yield strings, it yields lists of strings.

So we need to make two changes:

First,

lines1 = lineExtractor(file1)

becomes

lines1 = list(lineExtractor(file1))

Obviously, the same thing needs to be done with lines2.

Secondly,

yield lines

becomes yield ''.join(lines).