Use of Python difflib

Today, I made a content that is as similar as possible to the specified content from the content of the list, and take a few minutes to record it after finishing

The role of difflib

Compare the differences between 2 files.

When using it, just import difflib directly

get_close_matches function

Match the most similar content to return the result

list1 = ["abc", "acd", "adf", "bcd", "buff"]
str1 = "abc"
result = difflib.get_close_matches(str1, list1)
print(result)

print result:

['abc', 'bcd', 'acd']

There are some other functions in difflib as follows

 The role of context_diff

Return a difference text line, since the return is a list and the content cannot be seen, the following is converted to a string

import difflib

text1 = "abc"
text2 = "bcde"

res1 = difflib.context_diff(text1, text2)
print("".join(res1))

print result

 The role of ndiff

Returns the difference point of 2 files

import difflib

text1 = "abc"
text2 = "bcde"

res1 = difflib.ndiff(text1, text2)
print("".join(res1))

 It may be seen that - and + feel a little confused, don't panic, their functions are below

symbol meaning
‘-’ Included in the first series row, but not the second.
‘+’ Included in the second series row, but not the first.
’ ’ The two series lines agree.
‘?’ There are incremental differences.
‘^’ There are different characters.

Comparing this, we can understand that -a means that there is in text1, but not in text2, +d, +e means that there is in text2, but not in text1

The compare function of ndiff and difflib is the same

 Let's talk about the compare method of difflib.Differ()

Use of difflib's Differ

import difflib

text1 = "abc"
text2 = "bcde"

res1 = difflib.ndiff(text1, text2)
print("".join(res1))

d = difflib.Differ()
result = d.compare(text1, text2)
print("".join(result))

print result

 difflib's HtmlDiff method

import difflib

text1 = "abc"
text2 = "bcde"

res1 = difflib.ndiff(text1, text2)
print("".join(res1))
# Differ 比较差异
d = difflib.Differ()
result = d.compare(text1, text2)
print("".join(result))
# HtmlDiff 统计差异
d1 = difflib.HtmlDiff()
res2 = d1.make_file(text1, text2)
# 把比对结果写入到html中
with open("diff.html", "w") as f:
    f.write(res2)

Find diff.html in the same directory as the code and use the browser code

 You can clearly see the difference

difflib's SequenceMatcher method

import difflib

text1 = "abc"
text2 = "bcde"

res1 = difflib.ndiff(text1, text2)
print("".join(res1))
# Differ 比较差异
d = difflib.Differ()
result = d.compare(text1, text2)
print("".join(result))
# HtmlDiff 统计差异
d1 = difflib.HtmlDiff()
res2 = d1.make_file(text1, text2)
# 把比对结果写入到html中
with open("diff.html", "w") as f:
    f.write(res2)
# 2个文件的相似对
res3 = difflib.SequenceMatcher(None, text1, text2).quick_ratio()
print(res3)

print result

 

Guess you like

Origin blog.csdn.net/qq_33210042/article/details/131007868