-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNote_diff.py
More file actions
65 lines (54 loc) · 1.31 KB
/
Note_diff.py
File metadata and controls
65 lines (54 loc) · 1.31 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#!/usr/bin/env python
# encoding: utf-8
# product.py
# product
# Created by txooo on 2018/11/19
# Copyright © 2018 txooo. All rights reserved.
'''
Description
run cmdline :
pip install diff
'''
import difflib
tex1="""tex1:
this is a test for difflib ,just try to get difference of the log
现在试试功能是否可行 好呀
goodtest
那么试试吧好人
hkjhk
hkjhk
"""
tex1_lines=tex1.splitlines()
tex2="""tex2:
this is a test for difflib ,just try to get difference of the log
现在试试功能是否可行
goodtast
2
那么试试吧
khjk
hkjhk
"""
tex2_lines=tex2.splitlines()
#---------原始对比方法----------
#d=difflib.Differ()
#diff=d.compare(tex1_lines,tex2_lines)
#print '\n'.join(list(diff))
#--------html对比方法----------
#字符串对比
di = difflib.Differ()
diff = di.compare(tex1_lines, tex2_lines)
print('\n'.join(list(diff)))
print(len(list(diff)))
#生成html打哪
d= difflib.HtmlDiff()
q=d.make_file(tex1_lines,tex2_lines)
with open('diff.html', 'w') as f:
f.write(q)
# 输出相似度
def string_similar(s1, s2):
return difflib.SequenceMatcher(None, s1, s2).ratio()
similar = string_similar('你好', '你好啊')
similar1 = string_similar('a', 'a')
similar2 = string_similar('a', 'abc')
similar3 = string_similar('ae', 'abcd')
print('相似度:', similar, similar1, similar2, similar3)