5

I am working with python 3.4 in windows 7.Trying to compare two text files and i want to report the differences in them using difflib. Following is the code m using:

import difflib
from difflib_data import *

with open("s1.txt") as f, open("s2.txt") as g:
    flines = f.readlines()
    glines = g.readlines()

d = difflib.Differ()
diff = d.compare(flines, glines)
print("\n".join(diff))

Traceback: from difflib_data import * ImportError: No module named 'difflib_data'

How to remove this error....thanks

Maxxie
  • 397
  • 1
  • 4
  • 14
  • 2
    `difflib_data` is not a standard python's file/module. Possibly you copied the code from somewhere (?) Infact the code will run even if you remove the import `from difflib_data import *` line – sk11 Sep 17 '14 at 06:49

2 Answers2

3

From the following post, it seems it is the example data provided with the PyMOTW tutorial.

I assume the author wants you to copy and paste the source of test data into a new file named difflib_data.py in your working dir.

Copy the following lines into difflib_data.py

text1 = """Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integereu lacus accumsan arcu fermentum euismod. Donec pulvinar porttitortellus. Aliquam venenatis. Donec facilisis pharetra tortor. In necmauris eget magna consequat convallis. Nam sed sem vitae odiopellentesque interdum. Sed consequat viverra nisl. Suspendisse arcumetus, blandit quis, rhoncus ac, pharetra eget, velit. Maurisurna. Morbi nonummy molestie orci. Praesent nisi elit, fringilla ac,suscipit non, tristique vel, mauris. Curabitur vel lorem id nisl portaadipiscing. Suspendisse eu lectus. In nunc. Duis vulputate tristiqueenim. Donec quis lectus a justo imperdiet tempus."""

text1_lines = text1.splitlines()

text2 = """Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Integereu lacus accumsan arcu fermentum euismod. Donec pulvinar porttitortellus. Aliquam venenatis. Donec facilisis pharetra tortor. In necmauris eget magna consequat convallis. Nam sed sem vitae odiopellentesque interdum. Sed consequat viverra nisl. Suspendisse arcumetus, blandit quis, rhoncus ac, pharetra eget, velit. Maurisurna. Morbi nonummy molestie orci. Praesent nisi elit, fringilla ac,suscipit non, tristique vel, mauris. Curabitur vel lorem id nisl portaadipiscing. Suspendisse eu lectus. In nunc. Duis vulputate tristiqueenim. Donec quis lectus a justo imperdiet tempus."""

text2_lines = text2.splitlines()
Rishi Dua
  • 2,296
  • 2
  • 24
  • 35
  • thnks @Rishi Dua....it is just printing out the whole content of the files...could not find out the difference in them.... – Maxxie Sep 17 '14 at 07:01
  • Every line must be prefixed with -, +, ? etc. to mark the difference. – Rishi Dua Sep 17 '14 at 07:03
  • yeah a '+' is there at the starting of a different line...but how can i copy only these "+" lines to a different text file...?? – Maxxie Sep 17 '14 at 07:22
  • Lines prefixed with - indicate that they were in the first sequence, but not the second. Lines prefixed with + were in the second sequence, but not the first. You need to parse the values stored in "diff" if you want only the lines containing +. – Rishi Dua Sep 17 '14 at 07:28
0

To get rid of the exception "There is no module difflib_data", remove from difflib_data import * and replace

diff = d.compare(flines, glines)
print("\n".join(diff))

With this code:

diff = difflib.ndiff(text1_lines, text2_lines)
print('\n'.join(list(diff)))
Trasp
  • 1,132
  • 7
  • 11
Yugal
  • 21
  • 6