{"id":1149204,"date":"2025-01-13T16:46:47","date_gmt":"2025-01-13T08:46:47","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1149204.html"},"modified":"2025-01-13T16:46:50","modified_gmt":"2025-01-13T08:46:50","slug":"python%e5%a6%82%e4%bd%95%e8%ae%be%e7%bd%ae%e6%96%87%e4%bb%b6%e5%b7%ae%e5%bc%82","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1149204.html","title":{"rendered":"python\u5982\u4f55\u8bbe\u7f6e\u6587\u4ef6\u5dee\u5f02"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25175924\/c01fc684-32bf-4e91-9d01-86854d725e32.webp\" alt=\"python\u5982\u4f55\u8bbe\u7f6e\u6587\u4ef6\u5dee\u5f02\" \/><\/p>\n<p><p> <strong>Python\u8bbe\u7f6e\u6587\u4ef6\u5dee\u5f02\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u6587\u4ef6\u5bf9\u6bd4\u5e93\u3001\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3001\u8ba1\u7b97\u6587\u4ef6\u54c8\u5e0c\u503c\u3002<\/strong> \u5176\u4e2d\uff0c\u4f7f\u7528\u6587\u4ef6\u5bf9\u6bd4\u5e93\u662f\u6700\u5e38\u7528\u548c\u9ad8\u6548\u7684\u65b9\u6cd5\u4e4b\u4e00\u3002Python\u63d0\u4f9b\u4e86\u4e00\u4e9b\u5185\u7f6e\u5e93\u548c\u7b2c\u4e09\u65b9\u5e93\u6765\u5b9e\u73b0\u6587\u4ef6\u5dee\u5f02\u6bd4\u8f83\uff0c\u4f8b\u5982<code>difflib<\/code>\u548c<code>filecmp<\/code>\u5e93\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>difflib<\/code>\u5e93\u6765\u6bd4\u8f83\u4e24\u4e2a\u6587\u4ef6\u7684\u5dee\u5f02\u3002<\/p>\n<\/p>\n<p><p><code>difflib<\/code>\u5e93\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u4e00\u7ec4\u5de5\u5177\u6765\u8ba1\u7b97\u548c\u663e\u793a\u6587\u672c\u7684\u5dee\u5f02\u3002<code>difflib<\/code>\u5e93\u4e2d\u7684<code>unified_diff<\/code>\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u7edf\u4e00\u5dee\u5f02\u683c\u5f0f\u7684\u6587\u672c\u5dee\u5f02\uff0c\u8fd9\u662f\u4e00\u4e2a\u6bd4\u8f83\u76f4\u89c2\u4e14\u5bb9\u6613\u7406\u89e3\u7684\u683c\u5f0f\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u4f7f\u7528<code>difflib<\/code>\u5e93\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import difflib<\/p>\n<p>def compare_files(file1, file2):<\/p>\n<p>    with open(file1, &#39;r&#39;) as f1, open(file2, &#39;r&#39;) as f2:<\/p>\n<p>        file1_lines = f1.readlines()<\/p>\n<p>        file2_lines = f2.readlines()<\/p>\n<p>    diff = difflib.unified_diff(file1_lines, file2_lines, fromfile=file1, tofile=file2)<\/p>\n<p>    return &#39;&#39;.join(diff)<\/p>\n<p>file1 = &#39;file1.txt&#39;<\/p>\n<p>file2 = &#39;file2.txt&#39;<\/p>\n<p>diff = compare_files(file1, file2)<\/p>\n<p>print(diff)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>compare_files<\/code>\u51fd\u6570\u8bfb\u53d6\u4e24\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u5e76\u4f7f\u7528<code>difflib.unified_diff<\/code>\u51fd\u6570\u751f\u6210\u6587\u4ef6\u5dee\u5f02\u3002\u6700\u7ec8\u8fd4\u56de\u7684\u5dee\u5f02\u53ef\u4ee5\u76f4\u63a5\u6253\u5370\u6216\u4fdd\u5b58\u5230\u53e6\u4e00\u4e2a\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u5c06\u8be6\u7ec6\u4ecb\u7ecdPython\u8bbe\u7f6e\u6587\u4ef6\u5dee\u5f02\u7684\u5176\u4ed6\u65b9\u6cd5\u548c\u66f4\u591a\u5b9e\u9645\u5e94\u7528\u573a\u666f\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u6587\u4ef6\u5bf9\u6bd4\u5e93<\/h3>\n<\/p>\n<p><h4>1. <code>difflib<\/code>\u5e93<\/h4>\n<\/p>\n<p><p><code>difflib<\/code>\u5e93\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u4e00\u7ec4\u5de5\u5177\u6765\u8ba1\u7b97\u548c\u663e\u793a\u6587\u672c\u7684\u5dee\u5f02\u3002\u9664\u4e86<code>unified_diff<\/code>\u51fd\u6570\u5916\uff0c\u8fd8\u6709<code>context_diff<\/code>\u3001<code>ndiff<\/code>\u7b49\u51fd\u6570\u53ef\u4ee5\u751f\u6210\u4e0d\u540c\u683c\u5f0f\u7684\u6587\u672c\u5dee\u5f02\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import difflib<\/p>\n<p>def compare_files(file1, file2):<\/p>\n<p>    with open(file1, &#39;r&#39;) as f1, open(file2, &#39;r&#39;) as f2:<\/p>\n<p>        file1_lines = f1.readlines()<\/p>\n<p>        file2_lines = f2.readlines()<\/p>\n<p>    diff = difflib.context_diff(file1_lines, file2_lines, fromfile=file1, tofile=file2)<\/p>\n<p>    return &#39;&#39;.join(diff)<\/p>\n<p>file1 = &#39;file1.txt&#39;<\/p>\n<p>file2 = &#39;file2.txt&#39;<\/p>\n<p>diff = compare_files(file1, file2)<\/p>\n<p>print(diff)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>context_diff<\/code>\u51fd\u6570\u751f\u6210\u4e0a\u4e0b\u6587\u5dee\u5f02\u683c\u5f0f\u7684\u6587\u672c\u5dee\u5f02\u3002<\/p>\n<\/p>\n<p><h4>2. <code>filecmp<\/code>\u5e93<\/h4>\n<\/p>\n<p><p><code>filecmp<\/code>\u5e93\u4e5f\u662fPython\u6807\u51c6\u5e93\u7684\u4e00\u90e8\u5206\uff0c\u63d0\u4f9b\u4e86\u4e00\u7ec4\u5de5\u5177\u6765\u6bd4\u8f83\u6587\u4ef6\u548c\u76ee\u5f55\u3002<code>filecmp<\/code>\u5e93\u4e3b\u8981\u7528\u4e8e\u6bd4\u8f83\u6587\u4ef6\u5185\u5bb9\u662f\u5426\u76f8\u540c\uff0c\u800c\u4e0d\u662f\u751f\u6210\u8be6\u7ec6\u7684\u5dee\u5f02\u62a5\u544a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import filecmp<\/p>\n<p>def compare_files(file1, file2):<\/p>\n<p>    return filecmp.cmp(file1, file2, shallow=False)<\/p>\n<p>file1 = &#39;file1.txt&#39;<\/p>\n<p>file2 = &#39;file2.txt&#39;<\/p>\n<p>are_files_equal = compare_files(file1, file2)<\/p>\n<p>print(&#39;Files are equal:&#39;, are_files_equal)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>filecmp.cmp<\/code>\u51fd\u6570\u6bd4\u8f83\u4e24\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u662f\u5426\u76f8\u540c\uff0c<code>shallow<\/code>\u53c2\u6570\u4e3a<code>False<\/code>\u8868\u793a\u8fdb\u884c\u6df1\u5ea6\u6bd4\u8f83\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/h3>\n<\/p>\n<p><p>\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5e76\u6bd4\u8f83\u6bcf\u4e00\u884c\u662f\u53e6\u4e00\u79cd\u5b9e\u73b0\u6587\u4ef6\u5dee\u5f02\u7684\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u8f83\u5c0f\u7684\u6587\u4ef6\uff0c\u56e0\u4e3a\u5b83\u9700\u8981\u5c06\u6587\u4ef6\u5185\u5bb9\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def compare_files(file1, file2):<\/p>\n<p>    with open(file1, &#39;r&#39;) as f1, open(file2, &#39;r&#39;) as f2:<\/p>\n<p>        file1_lines = f1.readlines()<\/p>\n<p>        file2_lines = f2.readlines()<\/p>\n<p>    differences = []<\/p>\n<p>    for i, (line1, line2) in enumerate(zip(file1_lines, file2_lines)):<\/p>\n<p>        if line1 != line2:<\/p>\n<p>            differences.append((i, line1, line2))<\/p>\n<p>    return differences<\/p>\n<p>file1 = &#39;file1.txt&#39;<\/p>\n<p>file2 = &#39;file2.txt&#39;<\/p>\n<p>differences = compare_files(file1, file2)<\/p>\n<p>for diff in differences:<\/p>\n<p>    print(f&#39;Line {diff[0]}: {diff[1].strip()} != {diff[2].strip()}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>compare_files<\/code>\u51fd\u6570\u9010\u884c\u8bfb\u53d6\u4e24\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u5e76\u6bd4\u8f83\u6bcf\u4e00\u884c\uff0c\u5982\u679c\u4e0d\u76f8\u540c\u5219\u8bb0\u5f55\u5dee\u5f02\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u8ba1\u7b97\u6587\u4ef6\u54c8\u5e0c\u503c<\/h3>\n<\/p>\n<p><p>\u8ba1\u7b97\u6587\u4ef6\u54c8\u5e0c\u503c\u662f\u4e00\u79cd\u5feb\u901f\u6bd4\u8f83\u6587\u4ef6\u5185\u5bb9\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u8ba1\u7b97\u6587\u4ef6\u7684\u54c8\u5e0c\u503c\uff0c\u53ef\u4ee5\u5feb\u901f\u5224\u65ad\u4e24\u4e2a\u6587\u4ef6\u662f\u5426\u76f8\u540c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import hashlib<\/p>\n<p>def calculate_file_hash(file_path, hash_func=hashlib.md5):<\/p>\n<p>    hash_obj = hash_func()<\/p>\n<p>    with open(file_path, &#39;rb&#39;) as f:<\/p>\n<p>        while chunk := f.read(4096):<\/p>\n<p>            hash_obj.update(chunk)<\/p>\n<p>    return hash_obj.hexdigest()<\/p>\n<p>file1 = &#39;file1.txt&#39;<\/p>\n<p>file2 = &#39;file2.txt&#39;<\/p>\n<p>file1_hash = calculate_file_hash(file1)<\/p>\n<p>file2_hash = calculate_file_hash(file2)<\/p>\n<p>are_files_equal = file1_hash == file2_hash<\/p>\n<p>print(&#39;Files are equal:&#39;, are_files_equal)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>calculate_file_hash<\/code>\u51fd\u6570\u8ba1\u7b97\u6587\u4ef6\u7684\u54c8\u5e0c\u503c\uff0c\u9ed8\u8ba4\u4f7f\u7528<code>md5<\/code>\u54c8\u5e0c\u51fd\u6570\u3002\u901a\u8fc7\u6bd4\u8f83\u4e24\u4e2a\u6587\u4ef6\u7684\u54c8\u5e0c\u503c\uff0c\u53ef\u4ee5\u5feb\u901f\u5224\u65ad\u6587\u4ef6\u5185\u5bb9\u662f\u5426\u76f8\u540c\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5b9e\u9645\u5e94\u7528\u573a\u666f<\/h3>\n<\/p>\n<p><h4>1. \u914d\u7f6e\u6587\u4ef6\u6bd4\u8f83<\/h4>\n<\/p>\n<p><p>\u5728\u5f00\u53d1\u548c\u8fd0\u7ef4\u8fc7\u7a0b\u4e2d\uff0c\u7ecf\u5e38\u9700\u8981\u6bd4\u8f83\u914d\u7f6e\u6587\u4ef6\u7684\u5dee\u5f02\u3002\u53ef\u4ee5\u4f7f\u7528\u4e0a\u8ff0\u65b9\u6cd5\u6bd4\u8f83\u4e24\u4e2a\u914d\u7f6e\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u786e\u4fdd\u914d\u7f6e\u7684\u4e00\u81f4\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">config1 = &#39;config1.cfg&#39;<\/p>\n<p>config2 = &#39;config2.cfg&#39;<\/p>\n<p>diff = compare_files(config1, config2)<\/p>\n<p>print(diff)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u65e5\u5fd7\u6587\u4ef6\u6bd4\u8f83<\/h4>\n<\/p>\n<p><p>\u6bd4\u8f83\u65e5\u5fd7\u6587\u4ef6\u7684\u5dee\u5f02\u53ef\u4ee5\u5e2e\u52a9\u5b9a\u4f4d\u95ee\u9898\u548c\u5206\u6790\u7cfb\u7edf\u884c\u4e3a\u3002\u53ef\u4ee5\u4f7f\u7528\u9010\u884c\u6bd4\u8f83\u7684\u65b9\u6cd5\uff0c\u627e\u51fa\u65e5\u5fd7\u6587\u4ef6\u4e2d\u7684\u5dee\u5f02\u884c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">log1 = &#39;log1.log&#39;<\/p>\n<p>log2 = &#39;log2.log&#39;<\/p>\n<p>differences = compare_files(log1, log2)<\/p>\n<p>for diff in differences:<\/p>\n<p>    print(f&#39;Line {diff[0]}: {diff[1].strip()} != {diff[2].strip()}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u6570\u636e\u6587\u4ef6\u6bd4\u8f83<\/h4>\n<\/p>\n<p><p>\u5728\u6570\u636e\u5904\u7406\u548c\u5206\u6790\u8fc7\u7a0b\u4e2d\uff0c\u6bd4\u8f83\u6570\u636e\u6587\u4ef6\u7684\u5dee\u5f02\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u4e00\u81f4\u6027\u548c\u5b8c\u6574\u6027\u3002\u53ef\u4ee5\u4f7f\u7528\u6587\u4ef6\u54c8\u5e0c\u503c\u7684\u65b9\u6cd5\uff0c\u5feb\u901f\u5224\u65ad\u6570\u636e\u6587\u4ef6\u662f\u5426\u76f8\u540c\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data1 = &#39;data1.csv&#39;<\/p>\n<p>data2 = &#39;data2.csv&#39;<\/p>\n<p>data1_hash = calculate_file_hash(data1)<\/p>\n<p>data2_hash = calculate_file_hash(data2)<\/p>\n<p>are_files_equal = data1_hash == data2_hash<\/p>\n<p>print(&#39;Files are equal:&#39;, are_files_equal)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f18\u5316\u548c\u6269\u5c55<\/h3>\n<\/p>\n<p><h4>1. \u5904\u7406\u5927\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u9010\u884c\u8bfb\u53d6\u548c\u6bd4\u8f83\u7684\u65b9\u6cd5\uff0c\u907f\u514d\u5c06\u6574\u4e2a\u6587\u4ef6\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def compare_large_files(file1, file2):<\/p>\n<p>    differences = []<\/p>\n<p>    with open(file1, &#39;r&#39;) as f1, open(file2, &#39;r&#39;) as f2:<\/p>\n<p>        for i, (line1, line2) in enumerate(zip(f1, f2)):<\/p>\n<p>            if line1 != line2:<\/p>\n<p>                differences.append((i, line1, line2))<\/p>\n<p>    return differences<\/p>\n<p>file1 = &#39;large_file1.txt&#39;<\/p>\n<p>file2 = &#39;large_file2.txt&#39;<\/p>\n<p>differences = compare_large_files(file1, file2)<\/p>\n<p>for diff in differences:<\/p>\n<p>    print(f&#39;Line {diff[0]}: {diff[1].strip()} != {diff[2].strip()}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u591a\u7ebf\u7a0b\u548c\u5e76\u884c\u5904\u7406<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6587\u4ef6\u6bd4\u8f83\u4efb\u52a1\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u7ebf\u7a0b\u548c\u5e76\u884c\u5904\u7406\u7684\u65b9\u6cd5\uff0c\u63d0\u9ad8\u6bd4\u8f83\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import threading<\/p>\n<p>def compare_files_in_thread(file1, file2, differences, index):<\/p>\n<p>    with open(file1, &#39;r&#39;) as f1, open(file2, &#39;r&#39;) as f2:<\/p>\n<p>        for i, (line1, line2) in enumerate(zip(f1, f2)):<\/p>\n<p>            if line1 != line2:<\/p>\n<p>                differences[index].append((i, line1, line2))<\/p>\n<p>file1 = &#39;file1.txt&#39;<\/p>\n<p>file2 = &#39;file2.txt&#39;<\/p>\n<p>num_threads = 4<\/p>\n<p>differences = [[] for _ in range(num_threads)]<\/p>\n<p>threads = []<\/p>\n<p>for i in range(num_threads):<\/p>\n<p>    t = threading.Thread(target=compare_files_in_thread, args=(file1, file2, differences, i))<\/p>\n<p>    threads.append(t)<\/p>\n<p>    t.start()<\/p>\n<p>for t in threads:<\/p>\n<p>    t.join()<\/p>\n<p>all_differences = [diff for sublist in differences for diff in sublist]<\/p>\n<p>for diff in all_differences:<\/p>\n<p>    print(f&#39;Line {diff[0]}: {diff[1].strip()} != {diff[2].strip()}&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u4f7f\u7528\u591a\u7ebf\u7a0b\u5e76\u884c\u6bd4\u8f83\u6587\u4ef6\u5185\u5bb9\uff0c\u63d0\u9ad8\u6bd4\u8f83\u6548\u7387\u3002<\/p>\n<\/p>\n<p><h4>3. \u53ef\u89c6\u5316\u5dee\u5f02<\/h4>\n<\/p>\n<p><p>\u4e3a\u4e86\u66f4\u76f4\u89c2\u5730\u67e5\u770b\u6587\u4ef6\u5dee\u5f02\uff0c\u53ef\u4ee5\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u751f\u6210\u5dee\u5f02\u62a5\u544a\u3002\u4f8b\u5982\uff0c\u4f7f\u7528<code>difflib.HtmlDiff<\/code>\u751f\u6210HTML\u683c\u5f0f\u7684\u5dee\u5f02\u62a5\u544a\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import difflib<\/p>\n<p>def generate_diff_html(file1, file2, output_file):<\/p>\n<p>    with open(file1, &#39;r&#39;) as f1, open(file2, &#39;r&#39;) as f2:<\/p>\n<p>        file1_lines = f1.readlines()<\/p>\n<p>        file2_lines = f2.readlines()<\/p>\n<p>    d = difflib.HtmlDiff()<\/p>\n<p>    diff_html = d.make_file(file1_lines, file2_lines, fromdesc=file1, todesc=file2)<\/p>\n<p>    with open(output_file, &#39;w&#39;) as f:<\/p>\n<p>        f.write(diff_html)<\/p>\n<p>file1 = &#39;file1.txt&#39;<\/p>\n<p>file2 = &#39;file2.txt&#39;<\/p>\n<p>output_file = &#39;diff.html&#39;<\/p>\n<p>generate_diff_html(file1, file2, output_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c<code>generate_diff_html<\/code>\u51fd\u6570\u751f\u6210HTML\u683c\u5f0f\u7684\u6587\u4ef6\u5dee\u5f02\u62a5\u544a\uff0c\u5e76\u4fdd\u5b58\u5230\u6307\u5b9a\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u6765\u8bbe\u7f6e\u548c\u6bd4\u8f83\u6587\u4ef6\u5dee\u5f02\uff0c\u5305\u62ec\u4f7f\u7528\u6587\u4ef6\u5bf9\u6bd4\u5e93\u3001\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3001\u8ba1\u7b97\u6587\u4ef6\u54c8\u5e0c\u503c\u7b49\u3002\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u53d6\u51b3\u4e8e\u5177\u4f53\u7684\u5e94\u7528\u573a\u666f\u548c\u6587\u4ef6\u5927\u5c0f\u3002\u5bf9\u4e8e\u8f83\u5c0f\u7684\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>difflib<\/code>\u5e93\u751f\u6210\u8be6\u7ec6\u7684\u5dee\u5f02\u62a5\u544a\u3002\u5bf9\u4e8e\u8f83\u5927\u7684\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u9010\u884c\u8bfb\u53d6\u548c\u6bd4\u8f83\u7684\u65b9\u6cd5\uff0c\u907f\u514d\u5c06\u6574\u4e2a\u6587\u4ef6\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002\u901a\u8fc7\u591a\u7ebf\u7a0b\u548c\u5e76\u884c\u5904\u7406\uff0c\u53ef\u4ee5\u63d0\u9ad8\u6587\u4ef6\u6bd4\u8f83\u7684\u6548\u7387\u3002\u6b64\u5916\uff0c\u8fd8\u53ef\u4ee5\u751f\u6210HTML\u683c\u5f0f\u7684\u5dee\u5f02\u62a5\u544a\uff0c\u4fbf\u4e8e\u76f4\u89c2\u67e5\u770b\u6587\u4ef6\u5dee\u5f02\u3002\u5e0c\u671b\u672c\u6587\u63d0\u4f9b\u7684\u65b9\u6cd5\u548c\u793a\u4f8b\u4ee3\u7801\u80fd\u5e2e\u52a9\u60a8\u66f4\u597d\u5730\u8bbe\u7f6e\u548c\u6bd4\u8f83\u6587\u4ef6\u5dee\u5f02\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u6bd4\u8f83\u4e24\u4e2a\u6587\u4ef6\u7684\u5dee\u5f02\uff1f<\/strong><br \/>\u60a8\u53ef\u4ee5\u4f7f\u7528<code>difflib<\/code>\u6a21\u5757\u6765\u6bd4\u8f83\u4e24\u4e2a\u6587\u672c\u6587\u4ef6\u7684\u5dee\u5f02\u3002\u901a\u8fc7\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5e76\u4f7f\u7528<code>difflib.unified_diff()<\/code>\u65b9\u6cd5\uff0c\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u5305\u542b\u5dee\u5f02\u7684\u5217\u8868\u3002\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u9002\u7528\u4e8e\u6587\u672c\u6587\u4ef6\uff0c\u4e5f\u53ef\u4ee5\u7528\u4e8e\u4efb\u4f55\u7c7b\u578b\u7684\u6587\u4ef6\uff0c\u53ea\u8981\u60a8\u80fd\u591f\u5c06\u5b83\u4eec\u8f6c\u6362\u4e3a\u53ef\u6bd4\u8f83\u7684\u6587\u672c\u683c\u5f0f\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\uff0c\u5982\u4f55\u5ffd\u7565\u7a7a\u767d\u5b57\u7b26\u8fdb\u884c\u6587\u4ef6\u6bd4\u8f83\uff1f<\/strong><br \/>\u8981\u5728\u6bd4\u8f83\u6587\u4ef6\u65f6\u5ffd\u7565\u7a7a\u767d\u5b57\u7b26\uff0c\u60a8\u53ef\u4ee5\u5728\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u65f6\u4f7f\u7528<code>str.splitlines()<\/code>\u65b9\u6cd5\u6765\u5206\u5272\u884c\uff0c\u5e76\u4e14\u53ef\u4ee5\u4f7f\u7528<code>strip()<\/code>\u65b9\u6cd5\u53bb\u6389\u6bcf\u884c\u7684\u7a7a\u767d\u5b57\u7b26\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6bd4\u8f83\u7684\u51c6\u786e\u6027\uff0c\u907f\u514d\u56e0\u683c\u5f0f\u95ee\u9898\u800c\u4ea7\u751f\u8bef\u5dee\u3002<\/p>\n<p><strong>Python\u4e2d\u6709\u6ca1\u6709\u53ef\u89c6\u5316\u6587\u4ef6\u5dee\u5f02\u7684\u5de5\u5177\uff1f<\/strong><br \/>\u662f\u7684\uff0cPython\u6709\u591a\u4e2a\u5e93\u53ef\u4ee5\u5e2e\u52a9\u60a8\u53ef\u89c6\u5316\u6587\u4ef6\u7684\u5dee\u5f02\u3002\u4f8b\u5982\uff0c<code>difflib.HtmlDiff<\/code>\u7c7b\u53ef\u4ee5\u751f\u6210HTML\u683c\u5f0f\u7684\u5dee\u5f02\u62a5\u544a\uff0c\u60a8\u53ef\u4ee5\u5728\u6d4f\u89c8\u5668\u4e2d\u67e5\u770b\u3002\u6b64\u5916\uff0c\u8fd8\u6709\u4e00\u4e9b\u7b2c\u4e09\u65b9\u5e93\u5982<code>gitpython<\/code>\u548c<code>pandas<\/code>\uff0c\u5b83\u4eec\u4e5f\u53ef\u4ee5\u7528\u4e8e\u5904\u7406\u548c\u53ef\u89c6\u5316\u6587\u4ef6\u4e4b\u95f4\u7684\u5dee\u5f02\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u8bbe\u7f6e\u6587\u4ef6\u5dee\u5f02\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u6587\u4ef6\u5bf9\u6bd4\u5e93\u3001\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3001\u8ba1\u7b97\u6587\u4ef6\u54c8\u5e0c\u503c\u3002 \u5176\u4e2d\uff0c\u4f7f\u7528\u6587\u4ef6\u5bf9\u6bd4\u5e93\u662f [&hellip;]","protected":false},"author":3,"featured_media":1149209,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[37],"tags":[],"acf":[],"_links":{"self":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149204"}],"collection":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/users\/3"}],"replies":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/comments?post=1149204"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149204\/revisions"}],"predecessor-version":[{"id":1149211,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1149204\/revisions\/1149211"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1149209"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1149204"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1149204"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1149204"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}