A simply newbie question about ndiff

Neville Franks nospam-readonly at getsoft.com
Sun Apr 21 23:16:56 EDT 2002


Thanks, one small example that works is worth a thousand somethingoranothers
;-)

I'm a C++ developer and as I get older I find learning new languages doesn't
necessarily get any easier.

I'll go look at the source.


"David Goodger" <goodger at users.sourceforge.net> wrote in message
news:B8E8F587.21E4A%goodger at users.sourceforge.net...
> Neville Franks wrote:
> > I thought that Tim's original ndiff compared files, but the one in
difflib
> > compares lists of strings. Does this mean I somehow need to read the
files
> > into lists of strings and then call ndiff.
>
> Yes.  It's easy to turn a file into a list of strings.  Read with
> afile.readlines(), or split a multi-line string with astring.splitlines(1)
> (the 1 keeps the line endings).
>
> > A short example of how to use
> > ndiff to compare two files would be greatly appreciated.
>
> Use the source, Luke!  There are painfully detailed examples in there.
>
> Here's a quickie (untested, but oughta work)::
>
>     import difflib
>     list1 = open('file1').readlines()
>     list2 = open('file2').readlines()
>     diff = difflib.ndiff(list1, list2)
>     print ''.join(diff)
>
> --
> David Goodger  <goodger at users.sourceforge.net>  Open-source projects:
>   - Python Docutils: http://docutils.sourceforge.net/
>   - The Go Tools Project: http://gotools.sourceforge.net/
>





More information about the Python-list mailing list