{"id":1059863,"date":"2024-12-31T15:28:59","date_gmt":"2024-12-31T07:28:59","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1059863.html"},"modified":"2024-12-31T15:29:01","modified_gmt":"2024-12-31T07:29:01","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","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1059863.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-docs.pingcode.com\/wp-content\/uploads\/2024\/12\/1aeac17d-10dd-4dc3-8d59-303059cf457f.webp?x-oss-process=image\/auto-orient,1\/format,webp\" alt=\"python\u5982\u4f55\u8bb2\u4e24\u4e2a\u6587\u4ef6\u5408\u5e76\" \/><\/p>\n<p><p> <strong>Python\u5408\u5e76\u4e24\u4e2a\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u51e0\u79cd\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u6587\u4ef6\u8bfb\u53d6\u548c\u5199\u5165\u51fd\u6570\u3001\u4f7f\u7528Pandas\u5e93\u3001\u4ee5\u53ca\u4f7f\u7528OS\u5e93\u7b49\u3002<\/strong> \u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u4e00\u79cd\u901a\u8fc7\u6587\u4ef6\u8bfb\u53d6\u548c\u5199\u5165\u51fd\u6570\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f7f\u7528\u5185\u7f6e\u7684\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\uff0c\u7136\u540e\u5c06\u5176\u5199\u5165\u5230\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u4e2d\u3002\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5185\u5bb9\u6309\u987a\u5e8f\u5408\u5e76\uff0c\u4e14\u64cd\u4f5c\u7b80\u5355\u6613\u61c2\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u6587\u4ef6\u8bfb\u53d6\u548c\u5199\u5165\u51fd\u6570<\/h3>\n<\/p>\n<p><h4>1. \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u8981\u5c06\u4e24\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u5408\u5e76\uff0c\u6211\u4eec\u9996\u5148\u9700\u8981\u8bfb\u53d6\u8fd9\u4e24\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u3002\u53ef\u4ee5\u4f7f\u7528Python\u7684\u5185\u7f6e\u51fd\u6570<code>open()<\/code>\u6765\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>read()<\/code>\u65b9\u6cd5\u6765\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u7b2c\u4e00\u4e2a\u6587\u4ef6\u5e76\u8bfb\u53d6\u5185\u5bb9<\/p>\n<p>with open(&#39;file1.txt&#39;, &#39;r&#39;) as file1:<\/p>\n<p>    content1 = file1.read()<\/p>\n<h2><strong>\u6253\u5f00\u7b2c\u4e8c\u4e2a\u6587\u4ef6\u5e76\u8bfb\u53d6\u5185\u5bb9<\/strong><\/h2>\n<p>with open(&#39;file2.txt&#39;, &#39;r&#39;) as file2:<\/p>\n<p>    content2 = file2.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5408\u5e76\u6587\u4ef6\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u8bfb\u53d6\u4e86\u6587\u4ef6\u5185\u5bb9\u4e4b\u540e\uff0c\u6211\u4eec\u53ef\u4ee5\u5c06\u8fd9\u4e24\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u5408\u5e76\u6210\u4e00\u4e2a\u65b0\u7684\u5b57\u7b26\u4e32\u3002\u8fd9\u91cc\u53ef\u4ee5\u7b80\u5355\u5730\u4f7f\u7528\u5b57\u7b26\u4e32\u62fc\u63a5\u7684\u65b9\u5f0f\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5408\u5e76\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>merged_content = content1 + content2<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5199\u5165\u65b0\u7684\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u5c06\u5408\u5e76\u540e\u7684\u5185\u5bb9\u5199\u5165\u5230\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u4e2d\u3002\u6211\u4eec\u53ef\u4ee5\u518d\u6b21\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\uff0c\u5e76\u4f7f\u7528<code>write()<\/code>\u65b9\u6cd5\u6765\u5199\u5165\u5185\u5bb9\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u5408\u5e76\u540e\u7684\u5185\u5bb9\u5199\u5165\u65b0\u7684\u6587\u4ef6<\/p>\n<p>with open(&#39;merged_file.txt&#39;, &#39;w&#39;) as merged_file:<\/p>\n<p>    merged_file.write(merged_content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528Pandas\u5e93<\/h3>\n<\/p>\n<p><p>Pandas\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u636e\u5904\u7406\u5e93\uff0c\u9002\u7528\u4e8e\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\u3002\u5982\u679c\u4e24\u4e2a\u6587\u4ef6\u662fCSV\u683c\u5f0f\u7684\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528Pandas\u5e93\u6765\u5408\u5e76\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1. \u8bfb\u53d6CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f7f\u7528Pandas\u7684<code>read_csv()<\/code>\u51fd\u6570\u8bfb\u53d6CSV\u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u8bfb\u53d6\u7b2c\u4e00\u4e2aCSV\u6587\u4ef6<\/strong><\/h2>\n<p>df1 = pd.read_csv(&#39;file1.csv&#39;)<\/p>\n<h2><strong>\u8bfb\u53d6\u7b2c\u4e8c\u4e2aCSV\u6587\u4ef6<\/strong><\/h2>\n<p>df2 = pd.read_csv(&#39;file2.csv&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u5408\u5e76CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u4f7f\u7528Pandas\u7684<code>concat()<\/code>\u51fd\u6570\u5c06\u4e24\u4e2aDataFrame\u5408\u5e76\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5408\u5e76\u4e24\u4e2aDataFrame<\/p>\n<p>merged_df = pd.concat([df1, df2])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5199\u5165\u65b0\u7684CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5c06\u5408\u5e76\u540e\u7684DataFrame\u5199\u5165\u5230\u4e00\u4e2a\u65b0\u7684CSV\u6587\u4ef6\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u5408\u5e76\u540e\u7684DataFrame\u5199\u5165\u65b0\u7684CSV\u6587\u4ef6<\/p>\n<p>merged_df.to_csv(&#39;merged_file.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528OS\u5e93<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528OS\u5e93\u53ef\u4ee5\u5904\u7406\u66f4\u590d\u6742\u7684\u6587\u4ef6\u64cd\u4f5c\uff0c\u5982\u5408\u5e76\u591a\u4e2a\u6587\u4ef6\u5939\u4e2d\u7684\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a<\/p>\n<\/p>\n<p><h4>1. \u83b7\u53d6\u6587\u4ef6\u5217\u8868<\/h4>\n<\/p>\n<p><p>\u9996\u5148\uff0c\u4f7f\u7528<code>os.listdir()<\/code>\u83b7\u53d6\u6587\u4ef6\u5217\u8868\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<h2><strong>\u83b7\u53d6\u5f53\u524d\u76ee\u5f55\u4e0b\u7684\u6240\u6709\u6587\u4ef6<\/strong><\/h2>\n<p>file_list = os.listdir(&#39;.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>2. \u8bfb\u53d6\u5e76\u5408\u5e76\u6587\u4ef6\u5185\u5bb9<\/h4>\n<\/p>\n<p><p>\u904d\u5386\u6587\u4ef6\u5217\u8868\uff0c\u8bfb\u53d6\u6bcf\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u5e76\u5408\u5e76\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">merged_content = &#39;&#39;<\/p>\n<p>for file_name in file_list:<\/p>\n<p>    if os.path.isfile(file_name):<\/p>\n<p>        with open(file_name, &#39;r&#39;) as file:<\/p>\n<p>            merged_content += file.read()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h4>3. \u5199\u5165\u65b0\u7684\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u6700\u540e\uff0c\u5c06\u5408\u5e76\u540e\u7684\u5185\u5bb9\u5199\u5165\u5230\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\u4e2d\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u5c06\u5408\u5e76\u540e\u7684\u5185\u5bb9\u5199\u5165\u65b0\u7684\u6587\u4ef6<\/p>\n<p>with open(&#39;merged_file.txt&#39;, &#39;w&#39;) as merged_file:<\/p>\n<p>    merged_file.write(merged_content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u4ee5\u4e0a\u4ecb\u7ecd\u4e86\u4e09\u79cd\u5e38\u89c1\u7684\u5408\u5e76\u6587\u4ef6\u7684\u65b9\u6cd5\uff1a\u4f7f\u7528\u6587\u4ef6\u8bfb\u53d6\u548c\u5199\u5165\u51fd\u6570\u3001\u4f7f\u7528Pandas\u5e93\u3001\u4ee5\u53ca\u4f7f\u7528OS\u5e93\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u9002\u7528\u7684\u573a\u666f\u548c\u4f18\u7f3a\u70b9\u3002<strong>\u4f7f\u7528\u6587\u4ef6\u8bfb\u53d6\u548c\u5199\u5165\u51fd\u6570\u9002\u5408\u7b80\u5355\u7684\u6587\u4ef6\u5408\u5e76\u64cd\u4f5c\uff0c\u4f7f\u7528Pandas\u5e93\u9002\u5408\u5904\u7406\u7ed3\u6784\u5316\u6570\u636e\uff0c\u5982CSV\u6587\u4ef6\uff0c\u4f7f\u7528OS\u5e93\u9002\u5408\u5904\u7406\u66f4\u590d\u6742\u7684\u6587\u4ef6\u64cd\u4f5c\u3002<\/strong> \u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u66f4\u52a0\u9ad8\u6548\u5730\u5b8c\u6210\u6587\u4ef6\u5408\u5e76\u64cd\u4f5c\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\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\u6765\u5408\u5e76\u4e24\u4e2a\u6587\u672c\u6587\u4ef6\u3002\u9996\u5148\uff0c\u60a8\u53ef\u4ee5\u6253\u5f00\u4e24\u4e2a\u6587\u4ef6\uff0c\u5e76\u8bfb\u53d6\u5b83\u4eec\u7684\u5185\u5bb9\u3002\u63a5\u7740\uff0c\u53ef\u4ee5\u5c06\u8fd9\u4e24\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u5199\u5165\u4e00\u4e2a\u65b0\u7684\u6587\u4ef6\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_file.txt&#39;, &#39;w&#39;) as merged_file:\n    merged_file.write(file1.read())\n    merged_file.write(&quot;\\n&quot;)  # \u53ef\u9009\uff1a\u5728\u4e24\u4e2a\u6587\u4ef6\u4e4b\u95f4\u6dfb\u52a0\u6362\u884c\n    merged_file.write(file2.read())\n<\/code><\/pre>\n<p>\u6b64\u4ee3\u7801\u5c06<code>file1.txt<\/code>\u548c<code>file2.txt<\/code>\u7684\u5185\u5bb9\u5408\u5e76\u5230<code>merged_file.txt<\/code>\u4e2d\u3002<\/p>\n<p><strong>\u5728\u5408\u5e76\u6587\u4ef6\u65f6\u5982\u4f55\u5904\u7406\u7f16\u7801\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u5408\u5e76\u6587\u4ef6\u65f6\uff0c\u786e\u4fdd\u4f60\u4f7f\u7528\u6b63\u786e\u7684\u7f16\u7801\u683c\u5f0f\uff0c\u4ee5\u907f\u514d\u8bfb\u53d6\u65f6\u51fa\u73b0\u4e71\u7801\u95ee\u9898\u3002\u901a\u5e38\u60c5\u51b5\u4e0b\uff0cUTF-8\u662f\u63a8\u8350\u7684\u7f16\u7801\u683c\u5f0f\u3002\u53ef\u4ee5\u5728\u6253\u5f00\u6587\u4ef6\u65f6\u6307\u5b9a\u7f16\u7801\uff0c\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;file1.txt&#39;, &#39;r&#39;, encoding=&#39;utf-8&#39;) as file1:\n    ...\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u7684\u5185\u5bb9\u88ab\u6b63\u786e\u8bfb\u53d6\u548c\u5199\u5165\u3002<\/p>\n<p><strong>\u5408\u5e76CSV\u6587\u4ef6\u65f6\u9700\u8981\u6ce8\u610f\u54ea\u4e9b\u4e8b\u9879\uff1f<\/strong><br \/>\u5728\u5408\u5e76CSV\u6587\u4ef6\u65f6\uff0c\u8981\u786e\u4fdd\u6240\u6709\u6587\u4ef6\u7684\u5217\u6570\u548c\u987a\u5e8f\u4e00\u81f4\u3002\u5982\u679c\u6587\u4ef6\u4e2d\u7684\u5217\u4e0d\u4e00\u81f4\uff0c\u53ef\u80fd\u4f1a\u5bfc\u81f4\u6570\u636e\u6df7\u4e71\u3002\u4f7f\u7528<code>pandas<\/code>\u5e93\u53ef\u4ee5\u66f4\u65b9\u4fbf\u5730\u5408\u5e76CSV\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u4f7f\u7528<code>pandas<\/code>\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import pandas as pd\n\nfile1 = pd.read_csv(&#39;file1.csv&#39;)\nfile2 = pd.read_csv(&#39;file2.csv&#39;)\nmerged = pd.concat([file1, file2], ignore_index=True)\nmerged.to_csv(&#39;merged_file.csv&#39;, index=False)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4e0d\u4ec5\u7b80\u5316\u4e86\u5408\u5e76\u8fc7\u7a0b\uff0c\u8fd8\u80fd\u5904\u7406\u66f4\u590d\u6742\u7684\u6570\u636e\u60c5\u51b5\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5408\u5e76\u4e24\u4e2a\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u51e0\u79cd\uff0c\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\uff1a\u4f7f\u7528\u6587\u4ef6\u8bfb\u53d6\u548c\u5199\u5165\u51fd\u6570\u3001\u4f7f\u7528Pandas\u5e93\u3001\u4ee5\u53ca\u4f7f\u7528OS [&hellip;]","protected":false},"author":3,"featured_media":1059873,"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\/1059863"}],"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=1059863"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1059863\/revisions"}],"predecessor-version":[{"id":1059875,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1059863\/revisions\/1059875"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1059873"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1059863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1059863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1059863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}