{"id":1144258,"date":"2025-01-08T22:58:01","date_gmt":"2025-01-08T14:58:01","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1144258.html"},"modified":"2025-01-08T22:58:04","modified_gmt":"2025-01-08T14:58:04","slug":"%e5%a6%82%e4%bd%95%e5%b0%86%e5%87%a0%e4%b8%aa%e6%96%87%e4%bb%b6%e5%90%88%e5%b9%b6%e5%88%b0%e4%b8%80%e4%b8%aa%e6%96%87%e4%bb%b6python","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1144258.html","title":{"rendered":"\u5982\u4f55\u5c06\u51e0\u4e2a\u6587\u4ef6\u5408\u5e76\u5230\u4e00\u4e2a\u6587\u4ef6python"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24181437\/8bc8247b-5b6e-42a0-8fab-5df10b84ec5a.webp\" alt=\"\u5982\u4f55\u5c06\u51e0\u4e2a\u6587\u4ef6\u5408\u5e76\u5230\u4e00\u4e2a\u6587\u4ef6python\" \/><\/p>\n<p><p> <strong>\u5c06\u51e0\u4e2a\u6587\u4ef6\u5408\u5e76\u5230\u4e00\u4e2a\u6587\u4ef6\u7684Python\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5e38\u89c1\u7684\u5305\u62ec\u4f7f\u7528\u6587\u4ef6\u8bfb\u53d6\u3001\u5199\u5165\u64cd\u4f5c\u3001\u501f\u52a9Pandas\u5e93\u8fdb\u884c\u6570\u636e\u5904\u7406\u3001\u4f7f\u7528os\u5e93\u904d\u5386\u6587\u4ef6\u7b49\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u7684\u5b9e\u73b0\u6b65\u9aa4\u548c\u793a\u4f8b\u4ee3\u7801\u3002<\/strong><\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u57fa\u672c\u6587\u4ef6\u64cd\u4f5c<\/h3>\n<\/p>\n<p><p>\u6700\u57fa\u672c\u7684\u65b9\u6cd5\u662f\u4f7f\u7528Python\u5185\u7f6e\u7684\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570 <code>open()<\/code>, <code>read()<\/code>, <code>write()<\/code> \u8fdb\u884c\u6587\u4ef6\u7684\u8bfb\u53d6\u548c\u5199\u5165\u3002<\/p>\n<\/p>\n<p><h4>1.1\u3001\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5e76\u5199\u5165\u65b0\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5728\u8fd9\u4e00\u65b9\u6cd5\u4e2d\uff0c\u6211\u4eec\u5c06\u901a\u8fc7\u9010\u4e00\u8bfb\u53d6\u6bcf\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u5e76\u5c06\u5176\u5199\u5165\u5230\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6587\u4ef6\u5217\u8868<\/p>\n<p>file_list = [&#39;file1.txt&#39;, &#39;file2.txt&#39;, &#39;file3.txt&#39;]<\/p>\n<h2><strong>\u8f93\u51fa\u6587\u4ef6<\/strong><\/h2>\n<p>output_file = &#39;combined_file.txt&#39;<\/p>\n<p>with open(output_file, &#39;w&#39;) as outfile:<\/p>\n<p>    for fname in file_list:<\/p>\n<p>        with open(fname, &#39;r&#39;) as infile:<\/p>\n<p>            outfile.write(infile.read())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>1.2\u3001\u9644\u52a0\u6587\u4ef6\u5185\u5bb9\u5230\u65b0\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u662f\u5728\u6bcf\u6b21\u8bfb\u53d6\u4e00\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u540e\uff0c\u9644\u52a0\uff08append\uff09\u5230\u65b0\u7684\u6587\u4ef6\u4e2d\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6587\u4ef6\u5217\u8868<\/p>\n<p>file_list = [&#39;file1.txt&#39;, &#39;file2.txt&#39;, &#39;file3.txt&#39;]<\/p>\n<h2><strong>\u8f93\u51fa\u6587\u4ef6<\/strong><\/h2>\n<p>output_file = &#39;combined_file.txt&#39;<\/p>\n<p>with open(output_file, &#39;w&#39;) as outfile:<\/p>\n<p>    for fname in file_list:<\/p>\n<p>        with open(fname, &#39;r&#39;) as infile:<\/p>\n<p>            for line in infile:<\/p>\n<p>                outfile.write(line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Pandas\u8fdb\u884c\u6570\u636e\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u6587\u4ef6\u662f\u7ed3\u6784\u5316\u7684\uff0c\u5982CSV\u6587\u4ef6\uff0cPandas\u5e93\u662f\u4e00\u4e2a\u975e\u5e38\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u975e\u5e38\u65b9\u4fbf\u5730\u8fdb\u884c\u6570\u636e\u5408\u5e76\u3002<\/p>\n<\/p>\n<p><h4>2.1\u3001\u5408\u5e76CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u6587\u4ef6\u5217\u8868<\/strong><\/h2>\n<p>file_list = [&#39;file1.csv&#39;, &#39;file2.csv&#39;, &#39;file3.csv&#39;]<\/p>\n<h2><strong>\u8f93\u51fa\u6587\u4ef6<\/strong><\/h2>\n<p>output_file = &#39;combined_file.csv&#39;<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684DataFrame<\/strong><\/h2>\n<p>combined_df = pd.DataFrame()<\/p>\n<p>for file in file_list:<\/p>\n<p>    df = pd.read_csv(file)<\/p>\n<p>    combined_df = pd.concat([combined_df, df])<\/p>\n<h2><strong>\u5c06\u5408\u5e76\u540e\u7684DataFrame\u5199\u5165\u5230\u65b0\u7684CSV\u6587\u4ef6\u4e2d<\/strong><\/h2>\n<p>combined_df.to_csv(output_file, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2.2\u3001\u5408\u5e76Excel\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u6587\u4ef6\u5217\u8868<\/strong><\/h2>\n<p>file_list = [&#39;file1.xlsx&#39;, &#39;file2.xlsx&#39;, &#39;file3.xlsx&#39;]<\/p>\n<h2><strong>\u8f93\u51fa\u6587\u4ef6<\/strong><\/h2>\n<p>output_file = &#39;combined_file.xlsx&#39;<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2a\u7a7a\u7684DataFrame<\/strong><\/h2>\n<p>combined_df = pd.DataFrame()<\/p>\n<p>for file in file_list:<\/p>\n<p>    df = pd.read_excel(file)<\/p>\n<p>    combined_df = pd.concat([combined_df, df])<\/p>\n<h2><strong>\u5c06\u5408\u5e76\u540e\u7684DataFrame\u5199\u5165\u5230\u65b0\u7684Excel\u6587\u4ef6\u4e2d<\/strong><\/h2>\n<p>combined_df.to_excel(output_file, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528os\u5e93\u904d\u5386\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u9700\u8981\u5408\u5e76\u4e00\u4e2a\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528os\u5e93\u8fdb\u884c\u6587\u4ef6\u904d\u5386\u3002<\/p>\n<\/p>\n<p><h4>3.1\u3001\u5408\u5e76\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u672c\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u76ee\u5f55\u8def\u5f84<\/strong><\/h2>\n<p>directory = &#39;path\/to\/directory&#39;<\/p>\n<h2><strong>\u8f93\u51fa\u6587\u4ef6<\/strong><\/h2>\n<p>output_file = &#39;combined_file.txt&#39;<\/p>\n<p>with open(output_file, &#39;w&#39;) as outfile:<\/p>\n<p>    for filename in os.listdir(directory):<\/p>\n<p>        if filename.endswith(&#39;.txt&#39;):<\/p>\n<p>            with open(os.path.join(directory, filename), &#39;r&#39;) as infile:<\/p>\n<p>                outfile.write(infile.read())<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u5927\u6587\u4ef6\u548c\u5185\u5b58\u4f18\u5316<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0c\u76f4\u63a5\u8bfb\u53d6\u6574\u4e2a\u6587\u4ef6\u53ef\u80fd\u4f1a\u5bfc\u81f4\u5185\u5b58\u4e0d\u8db3\u95ee\u9898\uff0c\u53ef\u4ee5\u4f7f\u7528\u751f\u6210\u5668\u6765\u9010\u884c\u8bfb\u53d6\u548c\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>4.1\u3001\u4f7f\u7528\u751f\u6210\u5668\u9010\u884c\u8bfb\u53d6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6587\u4ef6\u5217\u8868<\/p>\n<p>file_list = [&#39;file1.txt&#39;, &#39;file2.txt&#39;, &#39;file3.txt&#39;]<\/p>\n<h2><strong>\u8f93\u51fa\u6587\u4ef6<\/strong><\/h2>\n<p>output_file = &#39;combined_file.txt&#39;<\/p>\n<p>def file_reader(file_list):<\/p>\n<p>    for fname in file_list:<\/p>\n<p>        with open(fname, &#39;r&#39;) as infile:<\/p>\n<p>            for line in infile:<\/p>\n<p>                yield line<\/p>\n<p>with open(output_file, &#39;w&#39;) as outfile:<\/p>\n<p>    for line in file_reader(file_list):<\/p>\n<p>        outfile.write(line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>4.2\u3001\u4f7f\u7528\u5185\u5b58\u6620\u5c04\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u6781\u5927\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u5b58\u6620\u5c04\u6587\u4ef6\uff08mmap\uff09\u6765\u63d0\u9ad8\u6548\u7387\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import mmap<\/p>\n<h2><strong>\u6587\u4ef6\u5217\u8868<\/strong><\/h2>\n<p>file_list = [&#39;file1.txt&#39;, &#39;file2.txt&#39;, &#39;file3.txt&#39;]<\/p>\n<h2><strong>\u8f93\u51fa\u6587\u4ef6<\/strong><\/h2>\n<p>output_file = &#39;combined_file.txt&#39;<\/p>\n<p>with open(output_file, &#39;w&#39;) as outfile:<\/p>\n<p>    for fname in file_list:<\/p>\n<p>        with open(fname, &#39;r+&#39;) as infile:<\/p>\n<p>            mmapped_file = mmap.mmap(infile.fileno(), 0)<\/p>\n<p>            outfile.write(mmapped_file.read().decode(&#39;utf-8&#39;))<\/p>\n<p>            mmapped_file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u5904\u7406\u4e0d\u540c\u6587\u4ef6\u683c\u5f0f\u7684\u5408\u5e76<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u5019\u9700\u8981\u5408\u5e76\u4e0d\u540c\u683c\u5f0f\u7684\u6587\u4ef6\uff0c\u5982JSON\u548cCSV\uff0c\u5904\u7406\u8fd9\u4e9b\u6587\u4ef6\u9700\u8981\u7279\u5b9a\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h4>5.1\u3001\u5408\u5e76JSON\u6587\u4ef6<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<h2><strong>\u6587\u4ef6\u5217\u8868<\/strong><\/h2>\n<p>file_list = [&#39;file1.json&#39;, &#39;file2.json&#39;, &#39;file3.json&#39;]<\/p>\n<h2><strong>\u8f93\u51fa\u6587\u4ef6<\/strong><\/h2>\n<p>output_file = &#39;combined_file.json&#39;<\/p>\n<p>combined_data = []<\/p>\n<p>for file in file_list:<\/p>\n<p>    with open(file, &#39;r&#39;) as infile:<\/p>\n<p>        data = json.load(infile)<\/p>\n<p>        combined_data.extend(data)<\/p>\n<p>with open(output_file, &#39;w&#39;) as outfile:<\/p>\n<p>    json.dump(combined_data, outfile)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>5.2\u3001\u5408\u5e76\u4e0d\u540c\u683c\u5f0f\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5408\u5e76\u4e0d\u540c\u683c\u5f0f\u6587\u4ef6\u9700\u8981\u6839\u636e\u5177\u4f53\u9700\u6c42\u8fdb\u884c\u5904\u7406\uff0c\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff0c\u6f14\u793a\u5982\u4f55\u5408\u5e76JSON\u548cCSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p>import json<\/p>\n<h2><strong>\u6587\u4ef6\u5217\u8868<\/strong><\/h2>\n<p>csv_file = &#39;file.csv&#39;<\/p>\n<p>json_file = &#39;file.json&#39;<\/p>\n<h2><strong>\u8f93\u51fa\u6587\u4ef6<\/strong><\/h2>\n<p>output_file = &#39;combined_file.csv&#39;<\/p>\n<h2><strong>\u8bfb\u53d6CSV\u6587\u4ef6<\/strong><\/h2>\n<p>csv_df = pd.read_csv(csv_file)<\/p>\n<h2><strong>\u8bfb\u53d6JSON\u6587\u4ef6<\/strong><\/h2>\n<p>with open(json_file, &#39;r&#39;) as infile:<\/p>\n<p>    json_data = json.load(infile)<\/p>\n<p>    json_df = pd.DataFrame(json_data)<\/p>\n<h2><strong>\u5408\u5e76DataFrame<\/strong><\/h2>\n<p>combined_df = pd.concat([csv_df, json_df])<\/p>\n<h2><strong>\u5199\u5165\u5230\u65b0\u7684CSV\u6587\u4ef6\u4e2d<\/strong><\/h2>\n<p>combined_df.to_csv(output_file, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u81ea\u52a8\u5316\u548c\u811a\u672c\u5316<\/h3>\n<\/p>\n<p><p>\u4e3a\u4e86\u4f7f\u6587\u4ef6\u5408\u5e76\u8fc7\u7a0b\u66f4\u52a0\u81ea\u52a8\u5316\uff0c\u53ef\u4ee5\u7f16\u5199\u4e00\u4e2a\u811a\u672c\uff0c\u5c06\u6587\u4ef6\u5408\u5e76\u4efb\u52a1\u5b9a\u65f6\u6267\u884c\u3002<\/p>\n<\/p>\n<p><h4>6.1\u3001\u5b9a\u65f6\u4efb\u52a1\u811a\u672c<\/h4>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528 <code>schedule<\/code> \u5e93\u6765\u8bbe\u7f6e\u5b9a\u65f6\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import schedule<\/p>\n<p>import time<\/p>\n<p>def merge_files():<\/p>\n<p>    # \u6587\u4ef6\u5408\u5e76\u903b\u8f91<\/p>\n<p>    file_list = [&#39;file1.txt&#39;, &#39;file2.txt&#39;, &#39;file3.txt&#39;]<\/p>\n<p>    output_file = &#39;combined_file.txt&#39;<\/p>\n<p>    with open(output_file, &#39;w&#39;) as outfile:<\/p>\n<p>        for fname in file_list:<\/p>\n<p>            with open(fname, &#39;r&#39;) as infile:<\/p>\n<p>                outfile.write(infile.read())<\/p>\n<h2><strong>\u6bcf\u5929\u6267\u884c\u4e00\u6b21\u4efb\u52a1<\/strong><\/h2>\n<p>schedule.every().day.at(&quot;00:00&quot;).do(merge_files)<\/p>\n<p>while True:<\/p>\n<p>    schedule.run_pending()<\/p>\n<p>    time.sleep(1)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u5f02\u5e38\u5904\u7406\u548c\u65e5\u5fd7\u8bb0\u5f55<\/h3>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u5408\u5e76\u8fc7\u7a0b\u4e2d\uff0c\u5f02\u5e38\u5904\u7406\u548c\u65e5\u5fd7\u8bb0\u5f55\u662f\u975e\u5e38\u91cd\u8981\u7684\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>logging<\/code> \u5e93\u6765\u8bb0\u5f55\u65e5\u5fd7\u3002<\/p>\n<\/p>\n<p><h4>7.1\u3001\u6dfb\u52a0\u65e5\u5fd7\u8bb0\u5f55<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<h2><strong>\u8bbe\u7f6e\u65e5\u5fd7<\/strong><\/h2>\n<p>logging.basicConfig(filename=&#39;file_merge.log&#39;, level=logging.INFO, <\/p>\n<p>                    format=&#39;%(asctime)s:%(levelname)s:%(message)s&#39;)<\/p>\n<p>def merge_files():<\/p>\n<p>    try:<\/p>\n<p>        file_list = [&#39;file1.txt&#39;, &#39;file2.txt&#39;, &#39;file3.txt&#39;]<\/p>\n<p>        output_file = &#39;combined_file.txt&#39;<\/p>\n<p>        with open(output_file, &#39;w&#39;) as outfile:<\/p>\n<p>            for fname in file_list:<\/p>\n<p>                with open(fname, &#39;r&#39;) as infile:<\/p>\n<p>                    outfile.write(infile.read())<\/p>\n<p>        logging.info(&#39;Files merged successfully&#39;)<\/p>\n<p>    except Exception as e:<\/p>\n<p>        logging.error(f&#39;Error occurred: {e}&#39;)<\/p>\n<p>merge_files()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u5c06\u591a\u4e2a\u6587\u4ef6\u5408\u5e76\u5230\u4e00\u4e2a\u6587\u4ef6\u4e2d\u3002\u65e0\u8bba\u662f\u7b80\u5355\u7684\u6587\u672c\u6587\u4ef6\uff0c\u8fd8\u662f\u7ed3\u6784\u5316\u7684\u6570\u636e\u6587\u4ef6\uff0cPython\u63d0\u4f9b\u4e86\u591a\u79cd\u7075\u6d3b\u7684\u89e3\u51b3\u65b9\u6848\u3002\u5e0c\u671b\u8fd9\u4e9b\u65b9\u6cd5\u80fd\u5e2e\u52a9\u4f60\u9ad8\u6548\u5730\u8fdb\u884c\u6587\u4ef6\u5904\u7406\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u4f7f\u7528Python\u5408\u5e76\u591a\u4e2a\u6587\u672c\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\u5c06\u591a\u4e2a\u6587\u672c\u6587\u4ef6\u5408\u5e76\u4e3a\u4e00\u4e2a\u6587\u4ef6\u3002\u9996\u5148\uff0c\u6253\u5f00\u76ee\u6807\u6587\u4ef6\u4ee5\u5199\u5165\u6a21\u5f0f\uff0c\u7136\u540e\u4f9d\u6b21\u8bfb\u53d6\u6bcf\u4e2a\u6e90\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u5e76\u5c06\u5176\u5199\u5165\u76ee\u6807\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">files = [&#39;file1.txt&#39;, &#39;file2.txt&#39;, &#39;file3.txt&#39;]\nwith open(&#39;merged_file.txt&#39;, &#39;w&#39;) as outfile:\n    for filename in files:\n        with open(filename) as infile:\n            outfile.write(infile.read())\n<\/code><\/pre>\n<p><strong>\u5408\u5e76\u6587\u4ef6\u65f6\u5982\u4f55\u5904\u7406\u6587\u4ef6\u7f16\u7801\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u5408\u5e76\u6587\u4ef6\u65f6\uff0c\u4e0d\u540c\u6587\u4ef6\u53ef\u80fd\u4f7f\u7528\u4e0d\u540c\u7684\u7f16\u7801\u683c\u5f0f\u3002\u4e3a\u4e86\u907f\u514d\u4e71\u7801\uff0c\u5efa\u8bae\u5728\u6253\u5f00\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801\u65b9\u5f0f\u3002\u4f8b\u5982\uff0c\u4f7f\u7528UTF-8\u7f16\u7801\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&#39;merged_file.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as outfile:\n    for filename in files:\n        with open(filename, encoding=&#39;utf-8&#39;) as infile:\n            outfile.write(infile.read())\n<\/code><\/pre>\n<p><strong>\u662f\u5426\u53ef\u4ee5\u4f7f\u7528Python\u5e93\u6765\u5408\u5e76\u6587\u4ef6\uff1f<\/strong><br \/>\u786e\u5b9e\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e9bPython\u5e93\u6765\u7b80\u5316\u6587\u4ef6\u5408\u5e76\u7684\u8fc7\u7a0b\u3002\u4f8b\u5982\uff0c<code>pandas<\/code>\u5e93\u9002\u7528\u4e8e\u5408\u5e76CSV\u6587\u4ef6\uff0c\u800c<code>PyPDF2<\/code>\u5e93\u53ef\u4ee5\u5408\u5e76PDF\u6587\u4ef6\u3002\u4f7f\u7528\u8fd9\u4e9b\u5e93\u80fd\u591f\u63d0\u4f9b\u66f4\u591a\u529f\u80fd\u548c\u7075\u6d3b\u6027\uff0c\u5904\u7406\u7279\u5b9a\u683c\u5f0f\u7684\u6587\u4ef6\u65f6\u5c24\u4e3a\u4fbf\u5229\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>pandas<\/code>\u5408\u5e76CSV\u6587\u4ef6\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\nfiles = [&#39;file1.csv&#39;, &#39;file2.csv&#39;, &#39;file3.csv&#39;]\ndataframes = [pd.read_csv(file) for file in files]\nmerged_dataframe = pd.concat(dataframes)\nmerged_dataframe.to_csv(&#39;merged_file.csv&#39;, index=False)\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"\u5c06\u51e0\u4e2a\u6587\u4ef6\u5408\u5e76\u5230\u4e00\u4e2a\u6587\u4ef6\u7684Python\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5e38\u89c1\u7684\u5305\u62ec\u4f7f\u7528\u6587\u4ef6\u8bfb\u53d6\u3001\u5199\u5165\u64cd\u4f5c\u3001\u501f\u52a9Pandas\u5e93\u8fdb\u884c\u6570\u636e [&hellip;]","protected":false},"author":3,"featured_media":1144263,"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\/1144258"}],"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=1144258"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1144258\/revisions"}],"predecessor-version":[{"id":1144265,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1144258\/revisions\/1144265"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1144263"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1144258"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1144258"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1144258"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}