{"id":1071476,"date":"2025-01-08T11:07:35","date_gmt":"2025-01-08T03:07:35","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1071476.html"},"modified":"2025-01-08T11:07:37","modified_gmt":"2025-01-08T03:07:37","slug":"python%e5%a6%82%e4%bd%95%e5%90%88%e5%b9%b6%e4%b8%a4%e4%b8%aa%e6%96%87%e6%a1%a3%e5%86%85%e5%ae%b9-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1071476.html","title":{"rendered":"python\u5982\u4f55\u5408\u5e76\u4e24\u4e2a\u6587\u6863\u5185\u5bb9"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25101945\/c4490645-1b95-4109-9ff8-1f13e733d464.webp\" alt=\"python\u5982\u4f55\u5408\u5e76\u4e24\u4e2a\u6587\u6863\u5185\u5bb9\" \/><\/p>\n<p><p> <strong>\u5408\u5e76\u4e24\u4e2a\u6587\u6863\u5185\u5bb9\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5982\u4f7f\u7528\u6587\u4ef6\u64cd\u4f5c\u3001\u5b57\u7b26\u4e32\u64cd\u4f5c\u3001\u5185\u7f6e\u5e93\u7b49\u3002\u5176\u4e2d\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Python\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\uff0c\u5c06\u4e24\u4e2a\u6587\u6863\u7684\u5185\u5bb9\u8bfb\u53d6\u5e76\u5199\u5165\u5230\u4e00\u4e2a\u65b0\u7684\u6587\u6863\u4e2d\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528Python\u6765\u5408\u5e76\u4e24\u4e2a\u6587\u6863\u7684\u5185\u5bb9\uff0c\u5e76\u4e14\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u6587\u4ef6\u64cd\u4f5c\u5408\u5e76\u6587\u6863\u5185\u5bb9<\/p>\n<\/p>\n<p><p>\u4f7f\u7528Python\u5185\u7f6e\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\uff0c\u53ef\u4ee5\u65b9\u4fbf\u5730\u8bfb\u53d6\u548c\u5199\u5165\u6587\u6863\u5185\u5bb9\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u7b2c\u4e00\u4e2a\u6587\u6863\uff0c\u8bfb\u53d6\u5176\u5185\u5bb9\u3002<\/li>\n<li>\u6253\u5f00\u7b2c\u4e8c\u4e2a\u6587\u6863\uff0c\u8bfb\u53d6\u5176\u5185\u5bb9\u3002<\/li>\n<li>\u521b\u5efa\u4e00\u4e2a\u65b0\u7684\u6587\u6863\uff0c\u5c06\u524d\u4e24\u4e2a\u6587\u6863\u7684\u5185\u5bb9\u5199\u5165\u65b0\u7684\u6587\u6863\u4e2d\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">def merge_files(file1, file2, output_file):<\/p>\n<p>    with open(file1, &#39;r&#39;, encoding=&#39;utf-8&#39;) as f1:<\/p>\n<p>        content1 = f1.read()<\/p>\n<p>    with open(file2, &#39;r&#39;, encoding=&#39;utf-8&#39;) as f2:<\/p>\n<p>        content2 = f2.read()<\/p>\n<p>    with open(output_file, &#39;w&#39;, encoding=&#39;utf-8&#39;) as of:<\/p>\n<p>        of.write(content1)<\/p>\n<p>        of.write(&quot;\\n&quot;)  # \u53ef\u9009\uff1a\u6dfb\u52a0\u6362\u884c\u7b26<\/p>\n<p>        of.write(content2)<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>merge_files(&#39;document1.txt&#39;, &#39;document2.txt&#39;, &#39;merged_document.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u5b57\u7b26\u4e32\u64cd\u4f5c\u5408\u5e76\u6587\u6863\u5185\u5bb9<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u6587\u4ef6\u64cd\u4f5c\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u64cd\u4f5c\u6765\u5408\u5e76\u6587\u6863\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff0c\u5c55\u793a\u5982\u4f55\u4f7f\u7528\u5b57\u7b26\u4e32\u64cd\u4f5c\u6765\u5408\u5e76\u4e24\u4e2a\u6587\u6863\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def merge_files_str(file1, file2, output_file):<\/p>\n<p>    with open(file1, &#39;r&#39;, encoding=&#39;utf-8&#39;) as f1:<\/p>\n<p>        content1 = f1.read()<\/p>\n<p>    with open(file2, &#39;r&#39;, encoding=&#39;utf-8&#39;) as f2:<\/p>\n<p>        content2 = f2.read()<\/p>\n<p>    merged_content = content1 + &quot;\\n&quot; + content2<\/p>\n<p>    with open(output_file, &#39;w&#39;, encoding=&#39;utf-8&#39;) as of:<\/p>\n<p>        of.write(merged_content)<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>merge_files_str(&#39;document1.txt&#39;, &#39;document2.txt&#39;, &#39;merged_document.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u5229\u7528Pandas\u5e93\u5408\u5e76\u6587\u6863\u5185\u5bb9<\/p>\n<\/p>\n<p><p>\u5982\u679c\u5904\u7406\u7684\u662f\u7ed3\u6784\u5316\u6570\u636e\uff0c\u4f8b\u5982CSV\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u6765\u5408\u5e76\u6587\u6863\u5185\u5bb9\u3002Pandas\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u529f\u80fd\uff0c\u9002\u5408\u5904\u7406\u6570\u636e\u91cf\u8f83\u5927\u7684\u6587\u6863\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>def merge_csv_files(file1, file2, output_file):<\/p>\n<p>    df1 = pd.read_csv(file1)<\/p>\n<p>    df2 = pd.read_csv(file2)<\/p>\n<p>    merged_df = pd.concat([df1, df2], ignore_index=True)<\/p>\n<p>    merged_df.to_csv(output_file, index=False)<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>merge_csv_files(&#39;data1.csv&#39;, &#39;data2.csv&#39;, &#39;merged_data.csv&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528pathlib\u5e93\u5408\u5e76\u6587\u6863\u5185\u5bb9<\/p>\n<\/p>\n<p><p>Python\u7684pathlib\u5e93\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u7cfb\u7edf\u8def\u5f84\u64cd\u4f5c\u65b9\u5f0f\uff0c\u7ed3\u5408\u6587\u4ef6\u64cd\u4f5c\u53ef\u4ee5\u7b80\u5316\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u7ef4\u62a4\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p>def merge_files_pathlib(file1, file2, output_file):<\/p>\n<p>    content1 = Path(file1).read_text(encoding=&#39;utf-8&#39;)<\/p>\n<p>    content2 = Path(file2).read_text(encoding=&#39;utf-8&#39;)<\/p>\n<p>    merged_content = content1 + &quot;\\n&quot; + content2<\/p>\n<p>    Path(output_file).write_text(merged_content, encoding=&#39;utf-8&#39;)<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>merge_files_pathlib(&#39;document1.txt&#39;, &#39;document2.txt&#39;, &#39;merged_document.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u5904\u7406\u5927\u6587\u4ef6\u65f6\u7684\u5408\u5e76\u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5408\u5e76\u7684\u6587\u6863\u975e\u5e38\u5927\uff0c\u53ef\u80fd\u65e0\u6cd5\u4e00\u6b21\u6027\u5168\u90e8\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002\u6b64\u65f6\u53ef\u4ee5\u9010\u884c\u8bfb\u53d6\u548c\u5199\u5165\uff0c\u786e\u4fdd\u5185\u5b58\u4f7f\u7528\u7387\u4f4e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def merge_large_files(file1, file2, output_file):<\/p>\n<p>    with open(output_file, &#39;w&#39;, encoding=&#39;utf-8&#39;) as of:<\/p>\n<p>        with open(file1, &#39;r&#39;, encoding=&#39;utf-8&#39;) as f1:<\/p>\n<p>            for line in f1:<\/p>\n<p>                of.write(line)<\/p>\n<p>        of.write(&quot;\\n&quot;)  # \u53ef\u9009\uff1a\u6dfb\u52a0\u6362\u884c\u7b26<\/p>\n<p>        with open(file2, &#39;r&#39;, encoding=&#39;utf-8&#39;) as f2:<\/p>\n<p>            for line in f2:<\/p>\n<p>                of.write(line)<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>merge_large_files(&#39;large_document1.txt&#39;, &#39;large_document2.txt&#39;, &#39;merged_large_document.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u5904\u7406\u4e0d\u540c\u7f16\u7801\u683c\u5f0f\u7684\u6587\u6863\u5408\u5e76<\/p>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e0d\u540c\u7f16\u7801\u683c\u5f0f\u7684\u6587\u6863\u65f6\uff0c\u9700\u8981\u786e\u4fdd\u6b63\u786e\u5904\u7406\u7f16\u7801\u95ee\u9898\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5904\u7406\u4e0d\u540c\u7f16\u7801\u683c\u5f0f\u7684\u6587\u6863\u5408\u5e76\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def merge_files_different_encoding(file1, encoding1, file2, encoding2, output_file, output_encoding):<\/p>\n<p>    with open(file1, &#39;r&#39;, encoding=encoding1) as f1:<\/p>\n<p>        content1 = f1.read()<\/p>\n<p>    with open(file2, &#39;r&#39;, encoding=encoding2) as f2:<\/p>\n<p>        content2 = f2.read()<\/p>\n<p>    merged_content = content1 + &quot;\\n&quot; + content2<\/p>\n<p>    with open(output_file, &#39;w&#39;, encoding=output_encoding) as of:<\/p>\n<p>        of.write(merged_content)<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>merge_files_different_encoding(&#39;document1_utf8.txt&#39;, &#39;utf-8&#39;, &#39;document2_iso.txt&#39;, &#39;ISO-8859-1&#39;, &#39;merged_document.txt&#39;, &#39;utf-8&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u5408\u5e76\u591a\u4e2a\u6587\u6863\u5185\u5bb9<\/p>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5408\u5e76\u591a\u4e2a\u6587\u6863\u5185\u5bb9\uff0c\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5408\u5e76\u591a\u4e2a\u6587\u6863\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def merge_multiple_files(file_list, output_file):<\/p>\n<p>    with open(output_file, &#39;w&#39;, encoding=&#39;utf-8&#39;) as of:<\/p>\n<p>        for file in file_list:<\/p>\n<p>            with open(file, &#39;r&#39;, encoding=&#39;utf-8&#39;) as f:<\/p>\n<p>                of.write(f.read())<\/p>\n<p>                of.write(&quot;\\n&quot;)  # \u53ef\u9009\uff1a\u6dfb\u52a0\u6362\u884c\u7b26<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>files_to_merge = [&#39;document1.txt&#39;, &#39;document2.txt&#39;, &#39;document3.txt&#39;]<\/p>\n<p>merge_multiple_files(files_to_merge, &#39;merged_document.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516b\u3001\u5408\u5e76\u6587\u6863\u5185\u5bb9\u5e76\u6dfb\u52a0\u6807\u8bb0<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u6211\u4eec\u9700\u8981\u5728\u5408\u5e76\u7684\u6587\u6863\u4e2d\u6dfb\u52a0\u6807\u8bb0\uff0c\u4ee5\u4fbf\u4e8e\u533a\u5206\u4e0d\u540c\u6765\u6e90\u7684\u5185\u5bb9\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u6dfb\u52a0\u6807\u8bb0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def merge_files_with_labels(file1, label1, file2, label2, output_file):<\/p>\n<p>    with open(file1, &#39;r&#39;, encoding=&#39;utf-8&#39;) as f1:<\/p>\n<p>        content1 = f&quot;{label1}\\n&quot; + f1.read()<\/p>\n<p>    with open(file2, &#39;r&#39;, encoding=&#39;utf-8&#39;) as f2:<\/p>\n<p>        content2 = f&quot;{label2}\\n&quot; + f2.read()<\/p>\n<p>    merged_content = content1 + &quot;\\n&quot; + content2<\/p>\n<p>    with open(output_file, &#39;w&#39;, encoding=&#39;utf-8&#39;) as of:<\/p>\n<p>        of.write(merged_content)<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>merge_files_with_labels(&#39;document1.txt&#39;, &#39;Document 1:&#39;, &#39;document2.txt&#39;, &#39;Document 2:&#39;, &#39;merged_document_with_labels.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e5d\u3001\u5408\u5e76\u7279\u5b9a\u5185\u5bb9\u7684\u6587\u6863<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ea\u9700\u8981\u5408\u5e76\u6587\u6863\u4e2d\u7684\u7279\u5b9a\u5185\u5bb9\uff0c\u4f8b\u5982\u5305\u542b\u67d0\u4e9b\u5173\u952e\u8bcd\u7684\u6bb5\u843d\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def merge_files_with_keywords(file1, keyword1, file2, keyword2, output_file):<\/p>\n<p>    def extract_content_with_keyword(file, keyword):<\/p>\n<p>        content = &quot;&quot;<\/p>\n<p>        with open(file, &#39;r&#39;, encoding=&#39;utf-8&#39;) as f:<\/p>\n<p>            for line in f:<\/p>\n<p>                if keyword in line:<\/p>\n<p>                    content += line<\/p>\n<p>        return content<\/p>\n<p>    content1 = extract_content_with_keyword(file1, keyword1)<\/p>\n<p>    content2 = extract_content_with_keyword(file2, keyword2)<\/p>\n<p>    merged_content = content1 + &quot;\\n&quot; + content2<\/p>\n<p>    with open(output_file, &#39;w&#39;, encoding=&#39;utf-8&#39;) as of:<\/p>\n<p>        of.write(merged_content)<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>merge_files_with_keywords(&#39;document1.txt&#39;, &#39;important&#39;, &#39;document2.txt&#39;, &#39;critical&#39;, &#39;merged_document_with_keywords.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5341\u3001\u5408\u5e76\u6587\u6863\u5185\u5bb9\u5e76\u6309\u987a\u5e8f\u6392\u5217<\/p>\n<\/p>\n<p><p>\u6709\u65f6\u9700\u8981\u5c06\u5408\u5e76\u7684\u6587\u6863\u5185\u5bb9\u6309\u7279\u5b9a\u987a\u5e8f\u6392\u5217\uff0c\u4f8b\u5982\u6309\u5b57\u6bcd\u987a\u5e8f\u6392\u5217\u3002\u4ee5\u4e0b\u793a\u4f8b\u5c55\u793a\u4e86\u5982\u4f55\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def merge_files_with_sorting(file1, file2, output_file):<\/p>\n<p>    with open(file1, &#39;r&#39;, encoding=&#39;utf-8&#39;) as f1:<\/p>\n<p>        content1 = f1.readlines()<\/p>\n<p>    with open(file2, &#39;r&#39;, encoding=&#39;utf-8&#39;) as f2:<\/p>\n<p>        content2 = f2.readlines()<\/p>\n<p>    merged_content = content1 + content2<\/p>\n<p>    merged_content.sort()<\/p>\n<p>    with open(output_file, &#39;w&#39;, encoding=&#39;utf-8&#39;) as of:<\/p>\n<p>        of.writelines(merged_content)<\/p>\n<h2><strong>\u793a\u4f8b\u7528\u6cd5<\/strong><\/h2>\n<p>merge_files_with_sorting(&#39;document1.txt&#39;, &#39;document2.txt&#39;, &#39;merged_sorted_document.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4e0a\u8ff0\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u5f0f\u6765\u5408\u5e76\u4e24\u4e2a\u6216\u591a\u4e2a\u6587\u6863\u7684\u5185\u5bb9\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u573a\u666f\u548c\u4f18\u7f3a\u70b9\uff0c\u6839\u636e\u5b9e\u9645\u60c5\u51b5\u8fdb\u884c\u9009\u62e9\uff0c\u53ef\u4ee5\u5b9e\u73b0\u9ad8\u6548\u3001\u4fbf\u6377\u7684\u6587\u6863\u5185\u5bb9\u5408\u5e76\u64cd\u4f5c\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5408\u5e76\u4e24\u4e2a\u6587\u672c\u6587\u4ef6\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u8981\u5408\u5e76\u4e24\u4e2a\u6587\u672c\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\u3002\u9996\u5148\uff0c\u6253\u5f00\u4e24\u4e2a\u6587\u4ef6\uff0c\u8bfb\u53d6\u5b83\u4eec\u7684\u5185\u5bb9\uff0c\u5e76\u5c06\u5185\u5bb9\u5199\u5165\u5230\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u4e2d\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;file1.txt&#39;, &#39;r&#39;) as file1, open(&#39;file2.txt&#39;, &#39;r&#39;) as file2, open(&#39;merged.txt&#39;, &#39;w&#39;) as merged_file:\n    merged_file.write(file1.read())\n    merged_file.write(&#39;\\n&#39;)  # \u6dfb\u52a0\u6362\u884c\u7b26\n    merged_file.write(file2.read())\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u786e\u4fdd\u4e86\u4e24\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u88ab\u987a\u5229\u5408\u5e76\u3002<\/p>\n<p><strong>\u5408\u5e76\u6587\u6863\u65f6\u5982\u4f55\u5904\u7406\u4e0d\u540c\u683c\u5f0f\u7684\u6587\u4ef6\uff1f<\/strong><br \/>\u5982\u679c\u9700\u8981\u5408\u5e76\u4e0d\u540c\u683c\u5f0f\u7684\u6587\u4ef6\uff0c\u4f8b\u5982\u4e00\u4e2a\u662f\u6587\u672c\u6587\u4ef6\uff0c\u53e6\u4e00\u4e2a\u662fWord\u6587\u6863\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684\u7b2c\u4e09\u65b9\u5e93\uff0c\u4f8b\u5982<code>python-docx<\/code>\u6765\u5904\u7406Word\u6587\u4ef6\u3002\u8bfb\u53d6Word\u6587\u6863\u7684\u5185\u5bb9\u540e\uff0c\u5c06\u5176\u8f6c\u6362\u4e3a\u6587\u672c\u5e76\u5408\u5e76\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">from docx import Document\n\ndef read_word_file(file_path):\n    doc = Document(file_path)\n    return &#39;\\n&#39;.join([para.text for para in doc.paragraphs])\n\ntext_content = &#39;&#39;\nwith open(&#39;file1.txt&#39;, &#39;r&#39;) as file1:\n    text_content += file1.read() + &#39;\\n&#39;\n\ntext_content += read_word_file(&#39;file2.docx&#39;)\n\nwith open(&#39;merged.txt&#39;, &#39;w&#39;) as merged_file:\n    merged_file.write(text_content)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5408\u5e76\u4e0d\u540c\u683c\u5f0f\u7684\u6587\u6863\u3002<\/p>\n<p><strong>\u5408\u5e76\u591a\u4e2a\u6587\u6863\u65f6\uff0c\u6709\u54ea\u4e9b\u6700\u4f73\u5b9e\u8df5\uff1f<\/strong><br \/>\u5728\u5408\u5e76\u591a\u4e2a\u6587\u6863\u65f6\uff0c\u9075\u5faa\u4e00\u4e9b\u6700\u4f73\u5b9e\u8df5\u53ef\u4ee5\u5e2e\u52a9\u63d0\u9ad8\u6548\u7387\u548c\u53ef\u8bfb\u6027\u3002\u9996\u5148\uff0c\u786e\u4fdd\u6240\u6709\u6587\u6863\u7684\u7f16\u7801\u683c\u5f0f\u4e00\u81f4\uff0c\u907f\u514d\u5b57\u7b26\u7f16\u7801\u95ee\u9898\u3002\u5176\u6b21\uff0c\u6dfb\u52a0\u9002\u5f53\u7684\u5206\u9694\u7b26\uff0c\u4ee5\u4fbf\u5728\u5408\u5e76\u540e\uff0c\u80fd\u6e05\u6670\u5730\u8fa8\u522b\u5404\u4e2a\u6587\u6863\u7684\u5185\u5bb9\u3002\u6700\u540e\uff0c\u4fdd\u7559\u5408\u5e76\u6587\u6863\u7684\u539f\u59cb\u683c\u5f0f\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u683c\u5f0f\u5316\u6587\u6863\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u76f8\u5173\u5e93\u6765\u4fdd\u6301\u683c\u5f0f\u4e00\u81f4\u6027\u3002\u8fd9\u6837\u505a\u4e0d\u4ec5\u53ef\u4ee5\u63d0\u9ad8\u5408\u5e76\u540e\u7684\u6587\u6863\u8d28\u91cf\uff0c\u8fd8\u80fd\u63d0\u5347\u540e\u7eed\u5904\u7406\u7684\u4fbf\u5229\u6027\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5408\u5e76\u4e24\u4e2a\u6587\u6863\u5185\u5bb9\u7684\u65b9\u6cd5\u6709\u5f88\u591a\u79cd\uff0c\u5982\u4f7f\u7528\u6587\u4ef6\u64cd\u4f5c\u3001\u5b57\u7b26\u4e32\u64cd\u4f5c\u3001\u5185\u7f6e\u5e93\u7b49\u3002\u5176\u4e2d\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Python\u7684\u6587 [&hellip;]","protected":false},"author":3,"featured_media":1071480,"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\/1071476"}],"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=1071476"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1071476\/revisions"}],"predecessor-version":[{"id":1071481,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1071476\/revisions\/1071481"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1071480"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1071476"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1071476"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1071476"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}