3.3.10 使用difflib.unified_diff函数

可以使用本函数来生成统一的差异结果文本,只要拿到第一个文本,再通过这个差异文本进行操作,就可以得到第二个文本,从而减少储存两份文件,缩小储存空间。

例子:

#python3.4.3

import difflib

diff = difflib.unified_diff('one\ntwo\nthree\n'.splitlines(keepends=True),
             'ore\ntwo\nthree\nemu\n'.splitlines(keepends=True))

for line in diff:
    print(line)

结果输出如下:

--- 

 

+++ 

 

@@ -1,3 +1,4 @@

 

-one

 

+ore

 

 two

 

 three

 

+emu

通过上面的结果来看,@这行是说明两个文件差异的多少,以及总行数,后面前面有加号的表示相对第一个文件要添加,前面有减号的表示相对第一个文件要删除,如果什么都没有表示不用改变。有这个差异化文件,可以生成第一个文件,也可以生成第二个文件,因为生成第一个文件时,只要碰到减号就不要变化,空白的行直接保留,加号的行去掉;第二个文件时刚好相反。从而达到保存一个文件,就可以生成两个文件的目的。

 

 

3.3.11创建像diff的工具

有了文本比较库的功能,来实现一个文件差异比较工具就是很容易的事情。

例子:

""" Command line interface to difflib.py providing diffs in four formats:

* ndiff:    lists every line and highlights interline changes.
* context:  highlights clusters of changes in a before/after format.
* unified:  highlights clusters of changes in an inline format.
* html:     generates side by side comparison with change highlights.

"""

import sys, os, time, difflib, optparse

def main():
     # Configure the option parser
    usage = "usage: %prog [options] fromfile tofile"
    parser = optparse.OptionParser(usage)
    parser.add_option("-c", action="store_true", default=False,
                      help='Produce a context format diff (default)')
    parser.add_option("-u", action="store_true", default=False,
                      help='Produce a unified format diff')
    hlp = 'Produce HTML side by side diff (can use -c and -l in conjunction)'
    parser.add_option("-m", action="store_true", default=False, help=hlp)
    parser.add_option("-n", action="store_true", default=False,
                      help='Produce a ndiff format diff')
    parser.add_option("-l", "--lines", type="int", default=3,
                      help='Set number of context lines (default 3)')
    (options, args) = parser.parse_args()

    if len(args) == 0:
        parser.print_help()
        sys.exit(1)
    if len(args) != 2:
        parser.error("need to specify both a fromfile and tofile")

    n = options.lines
    fromfile, tofile = args # as specified in the usage string

    # we're passing these as arguments to the diff function
    fromdate = time.ctime(os.stat(fromfile).st_mtime)
    todate = time.ctime(os.stat(tofile).st_mtime)
    with open(fromfile) as fromf, open(tofile) as tof:
        fromlines, tolines = list(fromf), list(tof)

    if options.u:
        diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile,
                                    fromdate, todate, n=n)
    elif options.n:
        diff = difflib.ndiff(fromlines, tolines)
    elif options.m:
        diff = difflib.HtmlDiff().make_file(fromlines, tolines, fromfile,
                                            tofile, context=options.c,
                                            numlines=n)
    else:
        diff = difflib.context_diff(fromlines, tolines, fromfile, tofile,
                                    fromdate, todate, n=n)

    # we're using writelines because diff is a generator
    sys.stdout.writelines(diff)

if __name__ == '__main__':
    main()

在这个例子里,可以使用不同的参数来选择产生比较后文本的结果方式,比如简单文本方式,也可以HTML方式,还可以HTML表格方式。




蔡军生  微信号: shenzhencai  深圳



发布了2031 篇原创文章 · 获赞 545 · 访问量 758万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 大白 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览