앞에서 복잡한 예제를 만들었었는데 여기에서는 보다 단순한 사용법 예제를 만들어 봤습니다.
예제가 되는 기본은 아래 링크를 참고 하였습니다.
https://docs.python.org/ko/3/library/difflib.html
여러종류의 함수들이 있는데 각각 동작상 차이가 많이 있기 때문에
예제코드 아래쪽에는 각각의 실행 결과도 주석으로 포함하였습니다.
비교시 가능하면 list 형태로 인자를 넘겨주며 list형태로 만들기 위해서 splitlines함수를 이용하였습니다. 그리고 다시 화면 출력을 위해서 list join을 해야하는데 개행이 splitlines하면서 사라졌기 때문에 join시 개행을 붙였습니다.
ndiff, Differ.compare() 는 거의 비슷한 출력을 보여주며 전체 라인에 달라지거나 사라진 정보등등 표현이 됩니다.
unified_diff는 ndiff와 비슷하긴 하나 전체 목록을 보여주지는 않고 달라진점 위 아래로 정보가 나오게 됩니다.
context_diff도 달라진 부분만 나오긴하나 좀더 구체적인 항목은 나오지 않고 다른 라인 정보만 나타납니다.
HtmlDiff()는 html 형태로 출력이 됩니다.
import difflib import sys from pprint import pprint text1 = " 1. Beautiful is \n 2. better \n 3. than ugly. \n 4. Explicit is better than implicit.\n 5. Simple is better than complex.\n 6. Complex is better than complicated.\n" text2 = " 1. Beautiful is \n 2. better \n 3. than ugly. \n 4. Explicit is better than implicit.\n 5. Simple is betr than complex.\n 6. Comaaaplex is better than compddlicated.\n" text1 = text1.splitlines() text2 = text2.splitlines() d = difflib.Differ() result = list(d.compare(text1, text2)) print("\n".join(result)) # 모든 리스트를 합칩니다.리스트를 합칠때 개행을 입력합니다. ''' 1. Beautiful is 2. better 3. than ugly. 4. Explicit is better than implicit. - 5. Simple is better than complex. ? -- + 5. Simple is betr than complex. - 6. Complex is better than complicated. + 6. Comaaaplex is better than compddlicated. ? +++ ++ ''' print("ndiff") '''a와 b(문자열의 리스트)를 비교합니다; Differ-스타일 델타(델타 줄을 생성하는 제너레이터)를 반환합니다.''' result = list(difflib.ndiff(text1, text2)) print("\n".join(result)) ''' ndiff 1. Beautiful is 2. better 3. than ugly. 4. Explicit is better than implicit. - 5. Simple is better than complex. ? -- + 5. Simple is betr than complex. - 6. Complex is better than complicated. + 6. Comaaaplex is better than compddlicated. ? +++ ++ ''' print("unified_diff") ''' a와 b(문자열의 리스트)를 비교합니다; 델타(델타 줄을 생성하는 제너레이터)를 통합 diff 형식으로 반환합니다. 통합(unified) diff는 단지 변경된 줄과 몇 줄의 문맥만을 더해서 표시하는 간결한 방법입니다. 변경 사항은 (별도의 이전/이후 블록 대신) 인라인 스타일로 표시됩니다. 문맥 줄의 수는 n에 의해 설정되며 기본값은 3입니다. ''' result = list(difflib.unified_diff(text1, text2)) print("\n".join(result)) ''' unified_diff --- +++ @@ -2,5 +2,5 @@ 2. better 3. than ugly. 4. Explicit is better than implicit. - 5. Simple is better than complex. - 6. Complex is better than complicated. + 5. Simple is betr than complex. + 6. Comaaaplex is better than compddlicated. ''' print("context_diff") result = list(difflib.context_diff(text1, text2)) print("\n".join(result)) ''' context_diff *** --- *************** *** 2,6 **** 2. better 3. than ugly. 4. Explicit is better than implicit. ! 5. Simple is better than complex. ! 6. Complex is better than complicated. --- 2,6 ---- 2. better 3. than ugly. 4. Explicit is better than implicit. ! 5. Simple is betr than complex. ! 6. Comaaaplex is better than compddlicated. ''' print("HtmlDiff") result = list(difflib.HtmlDiff().make_file(text1, text2)) print("".join(result)) ''' HtmlDiff <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title></title> <style type="text/css"> table.diff {font-family:Courier; border:medium;} .diff_header {background-color:#e0e0e0} td.diff_header {text-align:right} .diff_next {background-color:#c0c0c0} .diff_add {background-color:#aaffaa} .diff_chg {background-color:#ffff77} .diff_sub {background-color:#ffaaaa} </style> </head> <body> <table class="diff" id="difflib_chg_to0__top" cellspacing="0" cellpadding="0" rules="groups" > <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <colgroup></colgroup> <tbody> <tr><td class="diff_next" id="difflib_chg_to0__0"><a href="#difflib_chg_to0__0">f</a></td><td class="diff_header" id="from0_1">1</td><td nowrap="nowrap"> 1. Beautiful is </td><td class="diff_next"><a href="#difflib_chg_to0__0">f</a></td><td class="diff_header" id="to0_1">1</td><td nowrap="nowrap"> 1. Beautiful is </td></tr> <tr><td class="diff_next"></td><td class="diff_header" id="from0_2">2</td><td nowrap="nowrap"> 2. better </td><td class="diff_next"></td><td class="diff_header" id="to0_2">2</td><td nowrap="nowrap"> 2. better </td></tr> <tr><td class="diff_next"></td><td class="diff_header" id="from0_3">3</td><td nowrap="nowrap"> 3. than ugly. </td><td class="diff_next"></td><td class="diff_header" id="to0_3">3</td><td nowrap="nowrap"> 3. than ugly. </td></tr> <tr><td class="diff_next"></td><td class="diff_header" id="from0_4">4</td><td nowrap="nowrap"> 4. Explicit is better than implicit.</td><td class="diff_next"></td><td class="diff_header" id="to0_4">4</td><td nowrap="nowrap"> 4. Explicit is better than implicit.</td></tr> <tr><td class="diff_next"><a href="#difflib_chg_to0__top">t</a></td><td class="diff_header" id="from0_5">5</td><td nowrap="nowrap"> 5. Simple is bet<span class="diff_sub">te</span>r than complex.</td><td class="diff_next"><a href="#difflib_chg_to0__top">t</a></td><td class="diff_header" id="to0_5">5</td><td nowrap="nowrap"> 5. Simple is betr than complex.</td></tr> <tr><td class="diff_next"></td><td class="diff_header" id="from0_6">6</td><td nowrap="nowrap"> 6. Complex is better than complicated.</td><td class="diff_next"></td><td class="diff_header" id="to0_6">6</td><td nowrap="nowrap"> 6. Com<span class="diff_add">aaa</span>plex is better than comp<span class="diff_add">dd</span>licated.</td></tr> </tbody> </table> <table class="diff" summary="Legends"> <tr> <th colspan="2"> Legends </th> </tr> <tr> <td> <table border="" summary="Colors"> <tr><th> Colors </th> </tr> <tr><td class="diff_add"> Added </td></tr> <tr><td class="diff_chg">Changed</td> </tr> <tr><td class="diff_sub">Deleted</td> </tr> </table></td> <td> <table border="" summary="Links"> <tr><th colspan="2"> Links </th> </tr> <tr><td>(f)irst change</td> </tr> <tr><td>(n)ext change</td> </tr> <tr><td>(t)op</td> </tr> </table></td> </tr> </table> </body> </html> '''