我想做的事情类似于我认为变更控制系统所做的事情,它们比较两个文件,并在每次文件更改时保存一个小的差异。我一直在阅读这个页面:http://docs.python.org/library/difflib.html ,但显然没有理解。
我试图在下面展示的一个相对简单的程序中重新创建它,但我似乎缺少的是Delta至少包含与原始文件一样多的内容,甚至更多。有没有可能只获取纯粹的更改?我问的原因很明显-节省磁盘空间。我可以每次保存整个代码块,但最好只保存当前代码一次,然后保存更改的小差异。
我还在努力弄清楚为什么许多difflib函数返回生成器而不是列表,这有什么优势?
difflib适合我吗,还是我需要找到一个功能更多的专业软件包?
这意味着在第9行之后添加一行或多行。然后会出现像++REPLACE或++UPDATE这样的单词。http://www4.hawaii.gov/dags/icsd/ppmo/Stds_Web_Pages/pdf/it110401.pdf
我试图在下面展示的一个相对简单的程序中重新创建它,但我似乎缺少的是Delta至少包含与原始文件一样多的内容,甚至更多。有没有可能只获取纯粹的更改?我问的原因很明显-节省磁盘空间。我可以每次保存整个代码块,但最好只保存当前代码一次,然后保存更改的小差异。
我还在努力弄清楚为什么许多difflib函数返回生成器而不是列表,这有什么优势?
difflib适合我吗,还是我需要找到一个功能更多的专业软件包?
# Python Difflib demo
# Author: Neal Walters
# loosely based on http://ahlawat.net/wordpress/?p=371
# 01/17/2011
# build the files here - later we will just read the files probably
file1Contents="""
for j = 1 to 10:
print "ABC"
print "DEF"
print "HIJ"
print "JKL"
print "Hello World"
print "j=" + j
print "XYZ"
"""
file2Contents = """
for j = 1 to 10:
print "ABC"
print "DEF"
print "HIJ"
print "JKL"
print "Hello World"
print "XYZ"
print "The end"
"""
filename1 = "diff_file1.txt"
filename2 = "diff_file2.txt"
file1 = open(filename1,"w")
file2 = open(filename2,"w")
file1.write(file1Contents)
file2.write(file2Contents)
file1.close()
file2.close()
#end of file build
lines1 = open(filename1, "r").readlines()
lines2 = open(filename2, "r").readlines()
import difflib
print "\n FILE 1 \n"
for line in lines1:
print line
print "\n FILE 2 \n"
for line in lines2:
print line
diffSequence = difflib.ndiff(lines1, lines2)
print "\n ----- SHOW DIFF ----- \n"
for i, line in enumerate(diffSequence):
print line
diffObj = difflib.Differ()
deltaSequence = diffObj.compare(lines1, lines2)
deltaList = list(deltaSequence)
print "\n ----- SHOW DELTALIST ----- \n"
for i, line in enumerate(deltaList):
print line
#let's suppose we store just the diffSequence in the database
#then we want to take the current file (file2) and recreate the original (file1) from it
#by backward applying the diff
restoredFile1Lines = difflib.restore(diffSequence,1) # 1 indicates file1 of 2 used to create the diff
restoreFileList = list(restoredFile1Lines)
print "\n ----- SHOW REBUILD OF FILE1 ----- \n"
# this is not showing anything!
for i, line in enumerate(restoreFileList):
print line
谢谢!
更新:
contextDiffSeq = difflib.context_diff(lines1, lines2)
contextDiffList = list(contextDiffSeq)
print "\n ----- SHOW CONTEXTDIFF ----- \n"
for i, line in enumerate(contextDiffList):
print line
----- SHOW CONTEXTDIFF -----
* 5,9 **
print "HIJ" print "JKL" print "Hello World"
print "j=" + j
print "XYZ"
--- 5,9 ----
print "HIJ" print "JKL" print "Hello World" print "XYZ"
- print "The end"
另一个更新:
在大型机的源代码管理工具 Panvalet 和 Librarian 的旧时代,您可以像这样创建一个更改集:
++ADD 9
print "j=" + j
这意味着在第9行之后添加一行或多行。然后会出现像++REPLACE或++UPDATE这样的单词。http://www4.hawaii.gov/dags/icsd/ppmo/Stds_Web_Pages/pdf/it110401.pdf
difflib
实时计算它们。请告诉我你的决定(我需要一个借口花一天时间将JRCS.Diff翻译成Python)。 - Apalaladifflib
。例如,您可以尝试使用零行上下文的统一格式,并处理输出以使其更紧凑。请参见我的最新编辑示例。 - Apalala