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