[英]Python Diff Two Multiline Strings Like GitHub
I want to achieve a diff output like github's commit diff view . 我想实现像github的commit diff view这样的diff输出。 And I tried this:
我尝试了这个:
import difflib
first = """
def
baz
"""
second = """
deff
ba
bar
foo
"""
diff = ''
for text in difflib.unified_diff(first, second):
for prefix in ('---', '+++', '@@'):
if text.startswith(prefix):
break
else:
diff += text
The output is: 输出为:
d e f+f
b a-z
+b+a+r+
+f+o+o+
How can I achieve, 我该如何实现,
1 def+f
2 ba-z
+
3 bar
4 foo
# -
# 5 line
# 6 line
an output just like this. 这样的输出。 Thanks.
谢谢。
I'm not quite sure what format you mean with gitlab; 我不确定gitlab是什么格式; I've not seen char-by-char diffs in gitlab like your example.
我没有像您的示例那样在gitlab中看到逐字符差异。 If you want a more standardish line-by-line output, then I think you just have to pass lists to the diff function:
如果您想要更标准的逐行输出,那么我认为您只需要将列表传递给diff函数即可:
for text in difflib.unified_diff(first.split("\n"), second.split("\n")):
if text[:3] not in ('+++', '---', '@@ '):
print text
As every line is different in your example, diff is just going to see each line as having been totally changed and give you an output like: 由于示例中的每一行都不相同,因此diff只会将每一行视为已完全更改,并为您提供如下输出:
-def
-baz
+deff
+ba
+bar
+foo
If you want to do something more fancy, you can treat the data as a single string (as you were) and then try and split on new-lines. 如果您想做更多花哨的事情,可以将数据视为一个字符串(就像以前一样),然后尝试换行。 The return format seems to be
"{operation}{char}"
(including new line chars), so you can group and detect lines which all have the same operation and apply the correct logic. 返回格式似乎是
"{operation}{char}"
(包括换行符),因此您可以对所有具有相同操作的行进行分组和检测,并应用正确的逻辑。
I can't quite work out the rules you're trying to apply based on your example (are you grouping all mixed lines, then added lines then removed lines or something else?), so I can't give you an exact example. 我不能完全根据您的示例来确定您要应用的规则(您要对所有混合行进行分组,然后添加行,然后删除行还是其他?),所以我无法为您提供确切的示例。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.