3.3.10 使用difflib.unified

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

例子:

#python3.4.3import difflibdiff = 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

通过上面的结果来看,

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

例子:

""" 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, optparsedef main():# Configure the option parserusage = "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.linesfromfile, tofile = args # as specified in the usage string# we’re passing these as arguments to the diff functionfromdate = 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 generatorsys.stdout.writelines(diff)if __name__ == ‘__main__’:main()

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

蔡军生 微信号: shenzhencai 深圳

版权声明:本文为博主原创文章,未经博主允许不得转载。

一切都在发展变化,不断地向昨天告别,满怀信心地投入每一个崭新的今天。

3.3.10 使用difflib.unified

相关文章:

你感兴趣的文章:

标签云: