我试图找出一种方法来创建统一的差异,其中行号仅显示N
上下文行。我一直无法做到这一点difflib.unified_diff
。我需要显示两个文件中的更改。
我能来的最接近的是diff
在命令行上使用,如下所示:
/usr/bin/diff
--unchanged-line-format=' %.2dn %L'
--old-line-format="-%.2dn %L"
--new-line-format="+%.2dn %L"
file1.py
file2.py
但我只想显示N
上下文行,/usr/bin/diff
似乎不支持自定义行格式的上下文(例如,-U2 与 --line-format " conflicting output style options
" 不兼容)。
下面是我想要完成的示例(与上述差异相同的输出,但仅显示围绕更改的 1 行上下文):
+01: def renamed_function()
-01: def original_function():
02:
+03: """ Neat stuff here """
04:
21:
+22: # Here's a new comment
23:
85: # Output the value of foo()
+86: print "Foo is %s"%(foo())
-86: print foo()
87: