difflib.SequenceMatcher ( 유사도 )
from difflib import SequenceMatcher
def similar(a, b):
return round(float(SequenceMatcher(None, a, b).ratio()),2) * 100
print(similar('서울시 강남구','서울시 영등포구 '))
-----------------------------------------------------------------------
62.0
difflib.context_diff ( 문자열 목록 비교 )
samplefile_1.txt | samplefile_2.txt |
1 파이썬 2 python 3 iz 4 beautiful |
1 파이선 2 python 3 is 4 beautiful 5 6 diff |
from difflib import context_diff
with open('samplefile_1.txt', 'r') as f1:
with open('samplefile_2.txt', 'r') as f2:
diff = context_diff(f1.readlines(), f2.readlines(), fromfile='f1', tofile='f2')
for line in diff:
print(line)
------------------------------------------------------------------------------------------
*** f1
--- f2
***************
*** 1,4 ****
! 파이썬
python
! iz
beautiful
--- 1,6 ----
! 파이선 # file1과 file2 1 line 다른점
python
! is # file1과 file2 3 line 다른점
beautiful
+
# file2만 있는 내용
+ diff # file2만 있는 내용
'Python > Python Programming' 카테고리의 다른 글
[ Python ] ntplib 모듈을 이용한 시간 동기화 점검 (0) | 2023.05.14 |
---|---|
[ Python ] numpy를 이용한 1차원 배열 2 차원 배열로 변환 (0) | 2023.05.12 |
[ Python ] 날짜형식의 문자열 타입을 datetime 타입 형식으로 변환 (0) | 2023.05.03 |
[ Python ] xml 타입의 데이터 json 으로 변경 (0) | 2023.04.24 |
[ Python ] elastcisearch index 생성, 삭제, 조회 (0) | 2023.04.14 |
[ Python ] Remine API 사용하기 (0) | 2023.03.21 |
[ Python ] Tree Graph 만들기 (0) | 2023.02.15 |
[ Python ] eval과 literal_eval 차이 (0) | 2022.07.03 |