difflib and intelligent file differences

hayes.tyler at gmail.com hayes.tyler at gmail.com
Thu Mar 26 14:24:01 EDT 2009


Thanks for all of your suggestions. Turns out Marco's first version
was really the one I needed.

Thanks again,

t.

On Mar 26, 12:37 pm, Marco Mariani <ma... at sferacarta.com> wrote:
> Marco Mariani wrote:
> >> If the lines are really sorted, all you really need is a merge,
>
> For the archives, and for huge files where /usr/bin/diff or difflib are
> not appropriate, here it is.
>
> > #!/usr/bin/env python
>
> > import sys
>
> > def run(filea, fileb):
> >     p = 3
> >     while True:
> >         if p&1: a = filea.readline()
> >         if p&2: b = fileb.readline()
> >         if not a or not b:
> >             break
> >         elif a == b:
> >             p = 3
> >         elif a < b:
> >             sys.stdout.write('-%s' % a)
> >             p = 1
> >         elif b < a:
> >             sys.stdout.write('+%s' % b)
> >             p = 2
>
> >     for line in filea.readlines():
> >         sys.stdout.write('-%s' % line)
>
> >     for line in fileb.readlines():
> >         sys.stdout.write('+%s' % line)
>
> > if __name__ == '__main__':
> >     run(file(sys.argv[1]), file(sys.argv[2]))
>
>




More information about the Python-list mailing list