M. Manuel M. Manuel - 6 months ago 39
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

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

Could you please explain why i get this error? I've seen how to use the
but I do not get it because
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 = []
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


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:


lines1 = lineExtractor(file1)


lines1 = list(lineExtractor(file1))

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


yield lines

becomes yield ''.join(lines).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download