{"id":1095515,"date":"2025-01-08T14:51:51","date_gmt":"2025-01-08T06:51:51","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1095515.html"},"modified":"2025-01-08T14:51:53","modified_gmt":"2025-01-08T06:51:53","slug":"python%e5%a6%82%e4%bd%95%e8%ae%b2%e4%b8%a4%e4%b8%aa%e6%96%87%e4%bb%b6%e5%90%88%e5%b9%b6-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1095515.html","title":{"rendered":"python\u5982\u4f55\u8bb2\u4e24\u4e2a\u6587\u4ef6\u5408\u5e76"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24211133\/a7388ff9-5c6a-405e-bdb8-d4bf0abcbd4a.webp\" alt=\"python\u5982\u4f55\u8bb2\u4e24\u4e2a\u6587\u4ef6\u5408\u5e76\" \/><\/p>\n<p><p> <strong>Python\u5c06\u4e24\u4e2a\u6587\u4ef6\u5408\u5e76\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c\u3001\u4f7f\u7528pandas\u5e93\u3001\u4f7f\u7528fileinput\u6a21\u5757<\/strong>\u3002\u5728\u8fd9\u4e09\u79cd\u65b9\u6cd5\u4e2d\uff0c\u4f7f\u7528\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c\u662f\u6700\u57fa\u7840\u548c\u76f4\u63a5\u7684\u65b9\u6cd5\uff0c\u800c\u4f7f\u7528pandas\u5e93\u548cfileinput\u6a21\u5757\u53ef\u4ee5\u63d0\u4f9b\u66f4\u9ad8\u6548\u548c\u7b80\u6d01\u7684\u89e3\u51b3\u65b9\u6848\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4f7f\u7528\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528Python\u7684\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c\u6765\u5408\u5e76\u4e24\u4e2a\u6587\u4ef6\u662f\u6700\u57fa\u672c\u7684\u65b9\u6cd5\u3002\u6211\u4eec\u53ef\u4ee5\u6309\u4ee5\u4e0b\u6b65\u9aa4\u5b9e\u73b0\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u7b2c\u4e00\u4e2a\u6587\u4ef6\uff0c\u8bfb\u53d6\u5b83\u7684\u5185\u5bb9\u3002<\/li>\n<li>\u6253\u5f00\u7b2c\u4e8c\u4e2a\u6587\u4ef6\uff0c\u8bfb\u53d6\u5b83\u7684\u5185\u5bb9\u3002<\/li>\n<li>\u6253\u5f00\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\uff0c\u5199\u5165\u7b2c\u4e00\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u7136\u540e\u5199\u5165\u7b2c\u4e8c\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u3002<\/li>\n<\/ol>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def merge_files(file1, file2, output_file):<\/p>\n<p>    with open(file1, &#39;r&#39;) as f1, open(file2, &#39;r&#39;) as f2, open(output_file, &#39;w&#39;) as of:<\/p>\n<p>        content1 = f1.read()<\/p>\n<p>        content2 = f2.read()<\/p>\n<p>        of.write(content1)<\/p>\n<p>        of.write(content2)<\/p>\n<h2><strong>\u793a\u4f8b\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>file1 = &#39;file1.txt&#39;<\/p>\n<p>file2 = &#39;file2.txt&#39;<\/p>\n<p>output_file = &#39;merged_file.txt&#39;<\/p>\n<p>merge_files(file1, file2, output_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>open<\/code>\u51fd\u6570\u4ee5\u8bfb\u53d6\u6a21\u5f0f\uff08<code>&#39;r&#39;<\/code>\uff09\u6253\u5f00\u7b2c\u4e00\u4e2a\u6587\u4ef6\u548c\u7b2c\u4e8c\u4e2a\u6587\u4ef6\uff0c\u4ee5\u5199\u5165\u6a21\u5f0f\uff08<code>&#39;w&#39;<\/code>\uff09\u6253\u5f00\u8f93\u51fa\u6587\u4ef6\u3002\u8bfb\u53d6\u7b2c\u4e00\u4e2a\u6587\u4ef6\u548c\u7b2c\u4e8c\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u540e\uff0c\u5c06\u5b83\u4eec\u4f9d\u6b21\u5199\u5165\u8f93\u51fa\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528pandas\u5e93<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u6587\u4ef6\u662f\u6570\u636e\u6587\u4ef6\uff0c\u4f8b\u5982CSV\u6587\u4ef6\uff0c\u4f7f\u7528pandas\u5e93\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u8fdb\u884c\u5408\u5e76\u64cd\u4f5c\u3002pandas\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5904\u7406\u6570\u636e\u6587\u4ef6\u7684\u5408\u5e76\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5b89\u88c5pandas\u5e93\uff1a<code>pip install pandas<\/code><\/li>\n<li>\u4f7f\u7528pandas\u7684<code>read_csv<\/code>\u51fd\u6570\u8bfb\u53d6CSV\u6587\u4ef6\u3002<\/li>\n<li>\u4f7f\u7528<code>concat<\/code>\u51fd\u6570\u5408\u5e76\u6570\u636e\u3002<\/li>\n<li>\u4f7f\u7528<code>to_csv<\/code>\u51fd\u6570\u5c06\u5408\u5e76\u540e\u7684\u6570\u636e\u4fdd\u5b58\u5230\u65b0\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/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\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>file1 = &#39;file1.csv&#39;<\/p>\n<p>file2 = &#39;file2.csv&#39;<\/p>\n<p>output_file = &#39;merged_file.csv&#39;<\/p>\n<p>merge_csv_files(file1, file2, output_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>read_csv<\/code>\u51fd\u6570\u8bfb\u53d6CSV\u6587\u4ef6\u5e76\u8fd4\u56deDataFrame\u5bf9\u8c61\uff0c<code>concat<\/code>\u51fd\u6570\u7528\u4e8e\u5408\u5e76DataFrame\u5bf9\u8c61\uff0c<code>to_csv<\/code>\u51fd\u6570\u5c06\u5408\u5e76\u540e\u7684DataFrame\u5bf9\u8c61\u4fdd\u5b58\u5230\u65b0\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528fileinput\u6a21\u5757<\/h3>\n<\/p>\n<p><p>Python\u7684fileinput\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406\u591a\u4e2a\u6587\u4ef6\u7684\u8f93\u5165\u3002\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528fileinput\u6a21\u5757\u6765\u5408\u5e76\u4e24\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u3002<\/p>\n<\/p>\n<ol>\n<li>\u5bfc\u5165fileinput\u6a21\u5757\u3002<\/li>\n<li>\u4f7f\u7528fileinput.input\u51fd\u6570\u8bfb\u53d6\u591a\u4e2a\u6587\u4ef6\u3002<\/li>\n<li>\u5c06\u8bfb\u53d6\u7684\u5185\u5bb9\u5199\u5165\u65b0\u6587\u4ef6\u3002<\/li>\n<\/ol>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import fileinput<\/p>\n<p>def merge_files(file1, file2, output_file):<\/p>\n<p>    with open(output_file, &#39;w&#39;) as of:<\/p>\n<p>        for line in fileinput.input(files=(file1, file2)):<\/p>\n<p>            of.write(line)<\/p>\n<h2><strong>\u793a\u4f8b\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>file1 = &#39;file1.txt&#39;<\/p>\n<p>file2 = &#39;file2.txt&#39;<\/p>\n<p>output_file = &#39;merged_file.txt&#39;<\/p>\n<p>merge_files(file1, file2, output_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c<code>fileinput.input<\/code>\u51fd\u6570\u8bfb\u53d6\u591a\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\uff0c<code>open<\/code>\u51fd\u6570\u4ee5\u5199\u5165\u6a21\u5f0f\uff08<code>&#39;w&#39;<\/code>\uff09\u6253\u5f00\u8f93\u51fa\u6587\u4ef6\uff0c\u5c06\u8bfb\u53d6\u7684\u5185\u5bb9\u5199\u5165\u8f93\u51fa\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u5927\u6587\u4ef6\u7684\u4f18\u5316\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0c\u76f4\u63a5\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u5185\u5bb9\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u4e0d\u8db3\u7684\u95ee\u9898\u3002\u6211\u4eec\u53ef\u4ee5\u901a\u8fc7\u9010\u884c\u8bfb\u53d6\u548c\u5199\u5165\u7684\u65b9\u5f0f\u6765\u4f18\u5316\u5185\u5b58\u4f7f\u7528\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def merge_large_files(file1, file2, output_file):<\/p>\n<p>    with open(file1, &#39;r&#39;) as f1, open(file2, &#39;r&#39;) as f2, open(output_file, &#39;w&#39;) as of:<\/p>\n<p>        for line in f1:<\/p>\n<p>            of.write(line)<\/p>\n<p>        for line in f2:<\/p>\n<p>            of.write(line)<\/p>\n<h2><strong>\u793a\u4f8b\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>file1 = &#39;large_file1.txt&#39;<\/p>\n<p>file2 = &#39;large_file2.txt&#39;<\/p>\n<p>output_file = &#39;merged_large_file.txt&#39;<\/p>\n<p>merge_large_files(file1, file2, output_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5e76\u5199\u5165\u8f93\u51fa\u6587\u4ef6\uff0c\u907f\u514d\u4e86\u5185\u5b58\u4e0d\u8db3\u7684\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5408\u5e76\u591a\u4e2a\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5408\u5e76\u591a\u4e2a\u6587\u4ef6\uff0c\u53ef\u4ee5\u5c06\u6587\u4ef6\u540d\u5b58\u50a8\u5728\u5217\u8868\u4e2d\uff0c\u7136\u540e\u904d\u5386\u5217\u8868\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5e76\u5199\u5165\u8f93\u51fa\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def merge_multiple_files(files, output_file):<\/p>\n<p>    with open(output_file, &#39;w&#39;) as of:<\/p>\n<p>        for file in files:<\/p>\n<p>            with open(file, &#39;r&#39;) as f:<\/p>\n<p>                for line in f:<\/p>\n<p>                    of.write(line)<\/p>\n<h2><strong>\u793a\u4f8b\u6587\u4ef6\u8def\u5f84<\/strong><\/h2>\n<p>files = [&#39;file1.txt&#39;, &#39;file2.txt&#39;, &#39;file3.txt&#39;]<\/p>\n<p>output_file = &#39;merged_multiple_files.txt&#39;<\/p>\n<p>merge_multiple_files(files, output_file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u5c06\u6587\u4ef6\u540d\u5b58\u50a8\u5728<code>files<\/code>\u5217\u8868\u4e2d\uff0c\u904d\u5386\u5217\u8868\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5e76\u5199\u5165\u8f93\u51fa\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5408\u5e76\u6587\u4ef6\u7684\u5176\u4ed6\u6ce8\u610f\u4e8b\u9879<\/h3>\n<\/p>\n<p><p>\u5728\u5408\u5e76\u6587\u4ef6\u65f6\uff0c\u6709\u4e00\u4e9b\u5176\u4ed6\u6ce8\u610f\u4e8b\u9879\u9700\u8981\u8003\u8651\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u6587\u4ef6\u7f16\u7801<\/strong>\uff1a\u786e\u4fdd\u6240\u6709\u6587\u4ef6\u4f7f\u7528\u76f8\u540c\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u4f8b\u5982UTF-8\u3002\u5982\u679c\u6587\u4ef6\u4f7f\u7528\u4e0d\u540c\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u8bfb\u53d6\u548c\u5199\u5165\u9519\u8bef\u3002<\/li>\n<li><strong>\u6587\u4ef6\u683c\u5f0f<\/strong>\uff1a\u786e\u4fdd\u6240\u6709\u6587\u4ef6\u4f7f\u7528\u76f8\u540c\u7684\u683c\u5f0f\uff0c\u4f8b\u5982\u6587\u672c\u6587\u4ef6\u6216CSV\u6587\u4ef6\u3002\u5982\u679c\u6587\u4ef6\u4f7f\u7528\u4e0d\u540c\u7684\u683c\u5f0f\uff0c\u53ef\u80fd\u9700\u8981\u5bf9\u5185\u5bb9\u8fdb\u884c\u9884\u5904\u7406\u3002<\/li>\n<li><strong>\u6587\u4ef6\u8def\u5f84<\/strong>\uff1a\u5728\u4ee3\u7801\u4e2d\u4f7f\u7528\u7edd\u5bf9\u8def\u5f84\u6216\u76f8\u5bf9\u8def\u5f84\u65f6\uff0c\u786e\u4fdd\u8def\u5f84\u6b63\u786e\u3002\u5982\u679c\u6587\u4ef6\u8def\u5f84\u4e0d\u6b63\u786e\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6587\u4ef6\u65e0\u6cd5\u8bfb\u53d6\u6216\u5199\u5165\u3002<\/li>\n<\/ol>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u6211\u4eec\u53ef\u4ee5\u4f7f\u7528Python\u8f7b\u677e\u5730\u5408\u5e76\u4e24\u4e2a\u6587\u4ef6\u6216\u591a\u4e2a\u6587\u4ef6\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4f8b\u5982\u4f7f\u7528\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c\u3001pandas\u5e93\u6216fileinput\u6a21\u5757\u3002\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7\u9010\u884c\u8bfb\u53d6\u548c\u5199\u5165\u7684\u65b9\u5f0f\u4f18\u5316\u5185\u5b58\u4f7f\u7528\u3002\u5408\u5e76\u6587\u4ef6\u65f6\uff0c\u9700\u8981\u6ce8\u610f\u6587\u4ef6\u7f16\u7801\u3001\u683c\u5f0f\u548c\u8def\u5f84\u7b49\u95ee\u9898\u3002\u5e0c\u671b\u8fd9\u4e9b\u5185\u5bb9\u5bf9\u4f60\u6709\u6240\u5e2e\u52a9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5408\u5e76\u4e24\u4e2a\u6587\u672c\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\u5408\u5e76\u4e24\u4e2a\u6587\u672c\u6587\u4ef6\u975e\u5e38\u7b80\u5355\u3002\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\uff0c\u6253\u5f00\u4e24\u4e2a\u6587\u4ef6\uff0c\u9010\u884c\u8bfb\u53d6\u5185\u5bb9\uff0c\u5e76\u5c06\u5b83\u4eec\u5199\u5165\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u3002\u793a\u4f8b\u5982\u4e0b\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_file.txt&#39;, &#39;w&#39;) as merged_file:\n    for line in file1:\n        merged_file.write(line)\n    for line in file2:\n        merged_file.write(line)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c&#39;file1.txt&#39;\u548c&#39;file2.txt&#39;\u7684\u5185\u5bb9\u5c06\u4f1a\u88ab\u5408\u5e76\u5230&#39;merged_file.txt&#39;\u4e2d\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u5408\u5e76CSV\u6587\u4ef6\u7684\u6700\u4f73\u65b9\u6cd5\u662f\u4ec0\u4e48\uff1f<\/strong><br \/>\u5408\u5e76CSV\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528pandas\u5e93\u6765\u7b80\u5316\u64cd\u4f5c\u3002\u901a\u8fc7pandas\u8bfb\u53d6\u591a\u4e2aCSV\u6587\u4ef6\u5e76\u5c06\u5b83\u4eec\u5408\u5e76\u4e3a\u4e00\u4e2aDataFrame\uff0c\u7136\u540e\u518d\u5c06\u7ed3\u679c\u8f93\u51fa\u4e3a\u65b0\u7684CSV\u6587\u4ef6\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\ndf1 = pd.read_csv(&#39;file1.csv&#39;)\ndf2 = pd.read_csv(&#39;file2.csv&#39;)\nmerged_df = pd.concat([df1, df2])\nmerged_df.to_csv(&#39;merged_file.csv&#39;, index=False)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u5feb\u901f\u9ad8\u6548\u5730\u5408\u5e76CSV\u6587\u4ef6\uff0c\u540c\u65f6\u4fdd\u6301\u6570\u636e\u7ed3\u6784\u7684\u5b8c\u6574\u6027\u3002<\/p>\n<p><strong>\u5408\u5e76\u6587\u4ef6\u65f6\u5982\u4f55\u5904\u7406\u91cd\u590d\u5185\u5bb9\uff1f<\/strong><br \/>\u5728\u5408\u5e76\u6587\u4ef6\u65f6\uff0c\u5982\u679c\u9700\u8981\u53bb\u9664\u91cd\u590d\u5185\u5bb9\uff0c\u53ef\u4ee5\u5728\u5408\u5e76\u540e\u4f7f\u7528Python\u7684set\u6216pandas\u7684drop_duplicates()\u65b9\u6cd5\u6765\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528set\u53bb\u9664\u91cd\u590d\u884c\u7684\u793a\u4f8b\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_file.txt&#39;, &#39;w&#39;) as merged_file:\n    unique_lines = set(file1.readlines() + file2.readlines())\n    for line in unique_lines:\n        merged_file.write(line)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u7684\u65b9\u5f0f\u786e\u4fdd\u4e86\u5408\u5e76\u540e\u7684\u6587\u4ef6\u4e0d\u5305\u542b\u4efb\u4f55\u91cd\u590d\u7684\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5c06\u4e24\u4e2a\u6587\u4ef6\u5408\u5e76\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528\u6587\u4ef6\u8bfb\u5199\u64cd\u4f5c\u3001\u4f7f\u7528pandas\u5e93\u3001\u4f7f\u7528fileinput\u6a21\u5757\u3002\u5728\u8fd9\u4e09\u79cd\u65b9 [&hellip;]","protected":false},"author":3,"featured_media":1095525,"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\/1095515"}],"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=1095515"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095515\/revisions"}],"predecessor-version":[{"id":1095526,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095515\/revisions\/1095526"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1095525"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1095515"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1095515"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1095515"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}