Looking for a Duo - file comparison and a file parser

Robert Kern robert.kern at gmail.com
Wed Sep 10 02:00:21 CEST 2008


dudeja.rajat at gmail.com wrote:
> HI,
> 
> I'm thinking of writing a file comparison utility in python. This 
> utility will work as:
> 
> 1. Compare 2 files for the differences. Both files contain some 
> numerical results.
> 2. Generate a common file containing the differences (the same way as 
> 'diff' generate an output)
> 3. Now, I will parse this file containing differences to check as to how 
> much is the difference of one file from other. So, for this I hope a 
> parser will be required.
> 
> Is there a utility available that performs similar job ( that some one 
> might have created considering the huge number of users of python) ?
> 
> Or may be some one has written some parser?

There are a couple of ways to do #3. One would be to use the difflib module from 
the standard library. The Differ.compare() method will give you a sequence of 
lines with prefixed character saying whether the line was the same in both 
files, deleted from the first or added by the second. Lines that are recognized 
as having changed (rather than just being deleted then readded with 
modifications) are also noted. The parsing you would have to do would be minimal.

   http://docs.python.org/dev/library/difflib

Or you could use diff and diffstat:

   http://invisible-island.net/diffstat/

-- 
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
  that is made terrible by our own mad attempt to interpret it as though it had
  an underlying truth."
   -- Umberto Eco




More information about the Python-list mailing list