{"id":1095057,"date":"2025-01-08T14:47:22","date_gmt":"2025-01-08T06:47:22","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1095057.html"},"modified":"2025-01-08T14:47:28","modified_gmt":"2025-01-08T06:47:28","slug":"python%e5%a6%82%e4%bd%95%e5%9c%a8%e6%96%87%e4%bb%b6%e6%9c%80%e5%90%8e%e4%b8%80%e8%a1%8c-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1095057.html","title":{"rendered":"python\u5982\u4f55\u5728\u6587\u4ef6\u6700\u540e\u4e00\u884c"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/24210903\/ab809019-25e7-4d3d-93c4-efeabdca9e79.webp\" alt=\"python\u5982\u4f55\u5728\u6587\u4ef6\u6700\u540e\u4e00\u884c\" \/><\/p>\n<p><p> <strong>Python\u5728\u6587\u4ef6\u6700\u540e\u4e00\u884c\u6dfb\u52a0\u5185\u5bb9\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528\u8ffd\u52a0\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\u3001\u4f7f\u7528\u5e26\u6709 <code>a<\/code> \u6a21\u5f0f\u7684 <code>open<\/code> \u51fd\u6570\u3001\u901a\u8fc7 <code>write<\/code> \u65b9\u6cd5\u5199\u5165\u5185\u5bb9<\/strong>\u3002\u4ee5\u4e0b\u662f\u4e00\u4e9b\u8be6\u7ec6\u7684\u89e3\u91ca\u548c\u4ee3\u7801\u793a\u4f8b\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u8ffd\u52a0\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff1a<\/strong> \u5728 Python \u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u4ee5\u8ffd\u52a0\u6a21\u5f0f\uff08&#39;a&#39; \u6a21\u5f0f\uff09\u6253\u5f00\u6587\u4ef6\uff0c\u7136\u540e\u4f7f\u7528 <code>write<\/code> \u65b9\u6cd5\u5c06\u5185\u5bb9\u5199\u5165\u6587\u4ef6\u7684\u672b\u5c3e\u3002\u8ffd\u52a0\u6a21\u5f0f\u786e\u4fdd\u65b0\u5185\u5bb9\u88ab\u6dfb\u52a0\u5230\u6587\u4ef6\u7684\u6700\u540e\uff0c\u800c\u4e0d\u662f\u8986\u76d6\u73b0\u6709\u5185\u5bb9\u3002\u8fd9\u662f\u5b9e\u73b0\u6587\u4ef6\u672b\u5c3e\u6dfb\u52a0\u5185\u5bb9\u7684\u6700\u5e38\u89c1\u4e14\u7b80\u5355\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;a&#39;) as file:<\/p>\n<p>    file.write(&quot;\\nNew line at the end&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e00\u3001\u8ffd\u52a0\u6a21\u5f0f\uff08&#39;a&#39; \u6a21\u5f0f\uff09<\/p>\n<\/p>\n<p><p>\u8ffd\u52a0\u6a21\u5f0f\u662f Python \u4e2d\u6700\u5e38\u89c1\u7684\u6587\u4ef6\u64cd\u4f5c\u6a21\u5f0f\u4e4b\u4e00\u3002\u8ffd\u52a0\u6a21\u5f0f\u5141\u8bb8\u6211\u4eec\u5728\u4e0d\u8986\u76d6\u73b0\u6709\u5185\u5bb9\u7684\u60c5\u51b5\u4e0b\u5411\u6587\u4ef6\u6dfb\u52a0\u65b0\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528\u8ffd\u52a0\u6a21\u5f0f\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u6253\u5f00\u6587\u4ef6<\/strong> \u4f7f\u7528 <code>open<\/code> \u51fd\u6570\u5e76\u6307\u5b9a\u6a21\u5f0f\u4e3a <code>&#39;a&#39;<\/code>\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;a&#39;) as file:<\/p>\n<p>    file.write(&quot;\\nNew line at the end&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u5199\u5165\u65b0\u5185\u5bb9<\/strong> \u4f7f\u7528 <code>write<\/code> \u65b9\u6cd5\u5c06\u65b0\u5185\u5bb9\u6dfb\u52a0\u5230\u6587\u4ef6\u7684\u672b\u5c3e\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">file.write(&quot;\\nNew line at the end&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5173\u95ed\u6587\u4ef6<\/strong> \u4f7f\u7528 <code>close<\/code> \u65b9\u6cd5\u6216 <code>with<\/code> \u8bed\u53e5\u81ea\u52a8\u5173\u95ed\u6587\u4ef6\uff0c\u4ee5\u786e\u4fdd\u6240\u6709\u5185\u5bb9\u90fd\u5df2\u5199\u5165\u6587\u4ef6\u5e76\u91ca\u653e\u8d44\u6e90\u3002<\/li>\n<\/ol>\n<p><p>\u4e8c\u3001\u4f7f\u7528 <code>seek<\/code> \u51fd\u6570<\/p>\n<\/p>\n<p><p>\u6709\u65f6\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u66f4\u7cbe\u7ec6\u5730\u63a7\u5236\u6587\u4ef6\u6307\u9488\u7684\u4f4d\u7f6e\u3002\u5728\u8fd9\u79cd\u60c5\u51b5\u4e0b\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>seek<\/code> \u51fd\u6570\u5c06\u6587\u4ef6\u6307\u9488\u79fb\u52a8\u5230\u6587\u4ef6\u7684\u672b\u5c3e\uff0c\u7136\u540e\u4f7f\u7528 <code>write<\/code> \u65b9\u6cd5\u5199\u5165\u65b0\u5185\u5bb9\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u6253\u5f00\u6587\u4ef6<\/strong> \u4f7f\u7528 <code>open<\/code> \u51fd\u6570\u5e76\u6307\u5b9a\u6a21\u5f0f\u4e3a <code>&#39;r+&#39;<\/code>\uff08\u8bfb\u5199\u6a21\u5f0f\uff09\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r+&#39;) as file:<\/p>\n<p>    file.seek(0, os.SEEK_END)<\/p>\n<p>    file.write(&quot;\\nNew line at the end&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u79fb\u52a8\u6587\u4ef6\u6307\u9488<\/strong> \u4f7f\u7528 <code>seek<\/code> \u51fd\u6570\u5c06\u6587\u4ef6\u6307\u9488\u79fb\u52a8\u5230\u6587\u4ef6\u7684\u672b\u5c3e\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">file.seek(0, os.SEEK_END)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5199\u5165\u65b0\u5185\u5bb9<\/strong> \u4f7f\u7528 <code>write<\/code> \u65b9\u6cd5\u5c06\u65b0\u5185\u5bb9\u6dfb\u52a0\u5230\u6587\u4ef6\u7684\u672b\u5c3e\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">file.write(&quot;\\nNew line at the end&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u5173\u95ed\u6587\u4ef6<\/strong> \u4f7f\u7528 <code>close<\/code> \u65b9\u6cd5\u6216 <code>with<\/code> \u8bed\u53e5\u81ea\u52a8\u5173\u95ed\u6587\u4ef6\uff0c\u4ee5\u786e\u4fdd\u6240\u6709\u5185\u5bb9\u90fd\u5df2\u5199\u5165\u6587\u4ef6\u5e76\u91ca\u653e\u8d44\u6e90\u3002<\/li>\n<\/ol>\n<p><p>\u4e09\u3001\u4f7f\u7528 <code>readlines<\/code> \u65b9\u6cd5<\/p>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u60a8\u53ef\u80fd\u9700\u8981\u5728\u6587\u4ef6\u7684\u6700\u540e\u4e00\u884c\u4e4b\u540e\u6dfb\u52a0\u5185\u5bb9\uff0c\u5e76\u786e\u4fdd\u65b0\u5185\u5bb9\u4e0e\u73b0\u6709\u5185\u5bb9\u4e4b\u95f4\u6709\u4e00\u4e2a\u7a7a\u884c\u3002\u4e3a\u6b64\uff0c\u53ef\u4ee5\u4f7f\u7528 <code>readlines<\/code> \u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u7684\u6240\u6709\u884c\uff0c\u7136\u540e\u5728\u6700\u540e\u4e00\u884c\u4e4b\u540e\u6dfb\u52a0\u65b0\u5185\u5bb9\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong> \u4f7f\u7528 <code>readlines<\/code> \u65b9\u6cd5\u5c06\u6587\u4ef6\u7684\u6240\u6709\u884c\u8bfb\u53d6\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    lines = file.readlines()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u6dfb\u52a0\u65b0\u5185\u5bb9<\/strong> \u5c06\u65b0\u5185\u5bb9\u6dfb\u52a0\u5230\u5217\u8868\u7684\u672b\u5c3e\uff0c\u5e76\u786e\u4fdd\u65b0\u5185\u5bb9\u4e0e\u73b0\u6709\u5185\u5bb9\u4e4b\u95f4\u6709\u4e00\u4e2a\u7a7a\u884c\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">lines.append(&quot;\\nNew line at the end\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5199\u5165\u6587\u4ef6<\/strong> \u4f7f\u7528 <code>writelines<\/code> \u65b9\u6cd5\u5c06\u66f4\u65b0\u540e\u7684\u5185\u5bb9\u5199\u56de\u6587\u4ef6\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.writelines(lines)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u56db\u3001\u4f7f\u7528 <code>fileinput<\/code> \u6a21\u5757<\/p>\n<\/p>\n<p><p><code>fileinput<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u9010\u884c\u8bfb\u53d6\u548c\u5904\u7406\u6587\u4ef6\uff0c\u5e76\u5728\u6587\u4ef6\u7684\u6700\u540e\u4e00\u884c\u4e4b\u540e\u6dfb\u52a0\u65b0\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528 <code>fileinput<\/code> \u6a21\u5757\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165\u6a21\u5757<\/strong> \u5bfc\u5165 <code>fileinput<\/code> \u6a21\u5757\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import fileinput<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u9010\u884c\u5904\u7406\u6587\u4ef6<\/strong> \u4f7f\u7528 <code>fileinput.input<\/code> \u51fd\u6570\u9010\u884c\u8bfb\u53d6\u6587\u4ef6\uff0c\u5e76\u5728\u6700\u540e\u4e00\u884c\u4e4b\u540e\u6dfb\u52a0\u65b0\u5185\u5bb9\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">with fileinput.input(&#39;example.txt&#39;, inplace=True) as file:<\/p>\n<p>    for line in file:<\/p>\n<p>        print(line, end=&#39;&#39;)<\/p>\n<p>    print(&quot;\\nNew line at the end&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u5173\u95ed\u6587\u4ef6<\/strong> \u4f7f\u7528 <code>close<\/code> \u65b9\u6cd5\u81ea\u52a8\u5173\u95ed\u6587\u4ef6\uff0c\u4ee5\u786e\u4fdd\u6240\u6709\u5185\u5bb9\u90fd\u5df2\u5199\u5165\u6587\u4ef6\u5e76\u91ca\u653e\u8d44\u6e90\u3002<\/li>\n<\/ol>\n<p><p>\u4e94\u3001\u4f7f\u7528 <code>shutil<\/code> \u6a21\u5757<\/p>\n<\/p>\n<p><p><code>shutil<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u6cd5\u6765\u590d\u5236\u6587\u4ef6\u5185\u5bb9\u5e76\u5728\u6587\u4ef6\u7684\u6700\u540e\u4e00\u884c\u4e4b\u540e\u6dfb\u52a0\u65b0\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528 <code>shutil<\/code> \u6a21\u5757\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165\u6a21\u5757<\/strong> \u5bfc\u5165 <code>shutil<\/code> \u6a21\u5757\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import shutil<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u590d\u5236\u6587\u4ef6\u5185\u5bb9<\/strong> \u4f7f\u7528 <code>shutil.copyfile<\/code> \u51fd\u6570\u5c06\u6587\u4ef6\u5185\u5bb9\u590d\u5236\u5230\u4e00\u4e2a\u4e34\u65f6\u6587\u4ef6\u4e2d\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">shutil.copyfile(&#39;example.txt&#39;, &#39;example_temp.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6dfb\u52a0\u65b0\u5185\u5bb9<\/strong> \u4f7f\u7528 <code>open<\/code> \u51fd\u6570\u6253\u5f00\u4e34\u65f6\u6587\u4ef6\u5e76\u8ffd\u52a0\u65b0\u5185\u5bb9\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">with open(&#39;example_temp.txt&#39;, &#39;a&#39;) as file:<\/p>\n<p>    file.write(&quot;\\nNew line at the end&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u8986\u76d6\u539f\u6587\u4ef6<\/strong> \u4f7f\u7528 <code>shutil.move<\/code> \u51fd\u6570\u5c06\u4e34\u65f6\u6587\u4ef6\u8986\u76d6\u539f\u6587\u4ef6\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">shutil.move(&#39;example_temp.txt&#39;, &#39;example.txt&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516d\u3001\u4f7f\u7528 <code>pandas<\/code> \u6a21\u5757<\/p>\n<\/p>\n<p><p><code>pandas<\/code> \u6a21\u5757\u662f\u4e00\u4e2a\u529f\u80fd\u5f3a\u5927\u7684\u6570\u636e\u5206\u6790\u5de5\u5177\uff0c\u901a\u5e38\u7528\u4e8e\u5904\u7406\u8868\u683c\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528 <code>pandas<\/code> \u6a21\u5757\u5728\u6587\u4ef6\u7684\u6700\u540e\u4e00\u884c\u4e4b\u540e\u6dfb\u52a0\u65b0\u5185\u5bb9\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165\u6a21\u5757<\/strong> \u5bfc\u5165 <code>pandas<\/code> \u6a21\u5757\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong> \u4f7f\u7528 <code>pd.read_csv<\/code> \u51fd\u6570\u5c06\u6587\u4ef6\u5185\u5bb9\u8bfb\u53d6\u5230\u4e00\u4e2a DataFrame \u4e2d\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">df = pd.read_csv(&#39;example.txt&#39;, header=None)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6dfb\u52a0\u65b0\u5185\u5bb9<\/strong> \u4f7f\u7528 <code>df.append<\/code> \u65b9\u6cd5\u5c06\u65b0\u5185\u5bb9\u6dfb\u52a0\u5230 DataFrame \u7684\u672b\u5c3e\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">new_row = pd.DataFrame([[None, &quot;New line at the end&quot;]])<\/p>\n<p>df = df.append(new_row, ignore_index=True)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u5199\u5165\u6587\u4ef6<\/strong> \u4f7f\u7528 <code>df.to_csv<\/code> \u65b9\u6cd5\u5c06\u66f4\u65b0\u540e\u7684 DataFrame \u5199\u56de\u6587\u4ef6\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">df.to_csv(&#39;example.txt&#39;, header=False, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e03\u3001\u4f7f\u7528 <code>numpy<\/code> \u6a21\u5757<\/p>\n<\/p>\n<p><p><code>numpy<\/code> \u6a21\u5757\u662f\u4e00\u4e2a\u5f3a\u5927\u7684\u6570\u503c\u8ba1\u7b97\u5de5\u5177\uff0c\u901a\u5e38\u7528\u4e8e\u5904\u7406\u5927\u89c4\u6a21\u6570\u7ec4\u548c\u77e9\u9635\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528 <code>numpy<\/code> \u6a21\u5757\u5728\u6587\u4ef6\u7684\u6700\u540e\u4e00\u884c\u4e4b\u540e\u6dfb\u52a0\u65b0\u5185\u5bb9\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165\u6a21\u5757<\/strong> \u5bfc\u5165 <code>numpy<\/code> \u6a21\u5757\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import numpy as np<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong> \u4f7f\u7528 <code>np.loadtxt<\/code> \u51fd\u6570\u5c06\u6587\u4ef6\u5185\u5bb9\u8bfb\u53d6\u5230\u4e00\u4e2a\u6570\u7ec4\u4e2d\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">data = np.loadtxt(&#39;example.txt&#39;, dtype=str, delimiter=&quot;\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6dfb\u52a0\u65b0\u5185\u5bb9<\/strong> \u4f7f\u7528 <code>np.append<\/code> \u51fd\u6570\u5c06\u65b0\u5185\u5bb9\u6dfb\u52a0\u5230\u6570\u7ec4\u7684\u672b\u5c3e\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">data = np.append(data, &quot;New line at the end&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u5199\u5165\u6587\u4ef6<\/strong> \u4f7f\u7528 <code>np.savetxt<\/code> \u51fd\u6570\u5c06\u66f4\u65b0\u540e\u7684\u6570\u7ec4\u5199\u56de\u6587\u4ef6\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">np.savetxt(&#39;example.txt&#39;, data, fmt=&#39;%s&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u516b\u3001\u4f7f\u7528 <code>csv<\/code> \u6a21\u5757<\/p>\n<\/p>\n<p><p><code>csv<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u5904\u7406 CSV \u6587\u4ef6\uff0c\u5e76\u5728\u6587\u4ef6\u7684\u6700\u540e\u4e00\u884c\u4e4b\u540e\u6dfb\u52a0\u65b0\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528 <code>csv<\/code> \u6a21\u5757\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165\u6a21\u5757<\/strong> \u5bfc\u5165 <code>csv<\/code> \u6a21\u5757\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong> \u4f7f\u7528 <code>csv.reader<\/code> \u51fd\u6570\u5c06\u6587\u4ef6\u5185\u5bb9\u8bfb\u53d6\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:<\/p>\n<p>    reader = csv.reader(file)<\/p>\n<p>    data = list(reader)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6dfb\u52a0\u65b0\u5185\u5bb9<\/strong> \u5c06\u65b0\u5185\u5bb9\u6dfb\u52a0\u5230\u5217\u8868\u7684\u672b\u5c3e\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">data.append([&quot;New line at the end&quot;])<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u5199\u5165\u6587\u4ef6<\/strong> \u4f7f\u7528 <code>csv.writer<\/code> \u51fd\u6570\u5c06\u66f4\u65b0\u540e\u7684\u5185\u5bb9\u5199\u56de\u6587\u4ef6\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;w&#39;, newline=&#39;&#39;) as file:<\/p>\n<p>    writer = csv.writer(file)<\/p>\n<p>    writer.writerows(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e5d\u3001\u4f7f\u7528 <code>json<\/code> \u6a21\u5757<\/p>\n<\/p>\n<p><p><code>json<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u5904\u7406 JSON \u6587\u4ef6\uff0c\u5e76\u5728\u6587\u4ef6\u7684\u6700\u540e\u4e00\u884c\u4e4b\u540e\u6dfb\u52a0\u65b0\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528 <code>json<\/code> \u6a21\u5757\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165\u6a21\u5757<\/strong> \u5bfc\u5165 <code>json<\/code> \u6a21\u5757\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong> \u4f7f\u7528 <code>json.load<\/code> \u51fd\u6570\u5c06\u6587\u4ef6\u5185\u5bb9\u8bfb\u53d6\u5230\u4e00\u4e2a\u5217\u8868\u4e2d\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">with open(&#39;example.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = json.load(file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6dfb\u52a0\u65b0\u5185\u5bb9<\/strong> \u5c06\u65b0\u5185\u5bb9\u6dfb\u52a0\u5230\u5217\u8868\u7684\u672b\u5c3e\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">data.append({&quot;message&quot;: &quot;New line at the end&quot;})<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u5199\u5165\u6587\u4ef6<\/strong> \u4f7f\u7528 <code>json.dump<\/code> \u51fd\u6570\u5c06\u66f4\u65b0\u540e\u7684\u5185\u5bb9\u5199\u56de\u6587\u4ef6\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">with open(&#39;example.json&#39;, &#39;w&#39;) as file:<\/p>\n<p>    json.dump(data, file, indent=4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5341\u3001\u4f7f\u7528 <code>pathlib<\/code> \u6a21\u5757<\/p>\n<\/p>\n<p><p><code>pathlib<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u4e00\u79cd\u9762\u5411\u5bf9\u8c61\u7684\u65b9\u6cd5\u6765\u5904\u7406\u6587\u4ef6\u8def\u5f84\uff0c\u5e76\u5728\u6587\u4ef6\u7684\u6700\u540e\u4e00\u884c\u4e4b\u540e\u6dfb\u52a0\u65b0\u5185\u5bb9\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u4f7f\u7528 <code>pathlib<\/code> \u6a21\u5757\u7684\u8be6\u7ec6\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u5bfc\u5165\u6a21\u5757<\/strong> \u5bfc\u5165 <code>pathlib<\/code> \u6a21\u5757\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/strong> \u4f7f\u7528 <code>Path.read_text<\/code> \u65b9\u6cd5\u5c06\u6587\u4ef6\u5185\u5bb9\u8bfb\u53d6\u5230\u4e00\u4e2a\u5b57\u7b26\u4e32\u4e2d\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">file_path = Path(&#39;example.txt&#39;)<\/p>\n<p>content = file_path.read_text()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"3\">\n<li><strong>\u6dfb\u52a0\u65b0\u5185\u5bb9<\/strong> \u5c06\u65b0\u5185\u5bb9\u6dfb\u52a0\u5230\u5b57\u7b26\u4e32\u7684\u672b\u5c3e\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">content += &quot;\\nNew line at the end&quot;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"4\">\n<li><strong>\u5199\u5165\u6587\u4ef6<\/strong> \u4f7f\u7528 <code>Path.write_text<\/code> \u65b9\u6cd5\u5c06\u66f4\u65b0\u540e\u7684\u5185\u5bb9\u5199\u56de\u6587\u4ef6\uff1a<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\">file_path.write_text(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3<\/p>\n<\/p>\n<p><p>\u5728 Python \u4e2d\uff0c\u6709\u591a\u79cd\u65b9\u6cd5\u53ef\u4ee5\u5728\u6587\u4ef6\u7684\u6700\u540e\u4e00\u884c\u6dfb\u52a0\u5185\u5bb9\uff0c\u5305\u62ec\u4f7f\u7528\u8ffd\u52a0\u6a21\u5f0f\u3001<code>seek<\/code> \u51fd\u6570\u3001<code>readlines<\/code> \u65b9\u6cd5\u3001<code>fileinput<\/code> \u6a21\u5757\u3001<code>shutil<\/code> \u6a21\u5757\u3001<code>pandas<\/code> \u6a21\u5757\u3001<code>numpy<\/code> \u6a21\u5757\u3001<code>csv<\/code> \u6a21\u5757\u3001<code>json<\/code> \u6a21\u5757\u548c <code>pathlib<\/code> \u6a21\u5757\u3002\u6bcf\u79cd\u65b9\u6cd5\u90fd\u6709\u5176\u72ec\u7279\u7684\u4f18\u52bf\u548c\u9002\u7528\u573a\u666f\uff0c\u60a8\u53ef\u4ee5\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u672c\u6587\u7684\u8be6\u7ec6\u4ecb\u7ecd\u548c\u4ee3\u7801\u793a\u4f8b\uff0c\u60a8\u5e94\u8be5\u80fd\u591f\u8f7b\u677e\u638c\u63e1\u5982\u4f55\u5728 Python \u4e2d\u5b9e\u73b0\u6587\u4ef6\u672b\u5c3e\u6dfb\u52a0\u5185\u5bb9\u7684\u64cd\u4f5c\u3002\u65e0\u8bba\u662f\u5904\u7406\u7b80\u5355\u7684\u6587\u672c\u6587\u4ef6\uff0c\u8fd8\u662f\u590d\u6742\u7684 CSV\u3001JSON \u6587\u4ef6\uff0cPython \u90fd\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u5de5\u5177\u548c\u65b9\u6cd5\u6765\u6ee1\u8db3\u60a8\u7684\u9700\u6c42\u3002\u5e0c\u671b\u672c\u6587\u5bf9\u60a8\u6709\u6240\u5e2e\u52a9\uff0c\u5e76\u795d\u60a8\u5728\u4f7f\u7528 Python \u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u65f6\u53d6\u5f97\u6210\u529f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u6253\u5f00\u5e76\u8bfb\u53d6\u6587\u4ef6\u7684\u6700\u540e\u4e00\u884c\uff1f<\/strong><br \/>\u8981\u5728Python\u4e2d\u6253\u5f00\u6587\u4ef6\u5e76\u8bfb\u53d6\u6700\u540e\u4e00\u884c\uff0c\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;yourfile.txt&#39;, &#39;rb&#39;) as f:\n    f.seek(-2, 2)  # \u4ece\u6587\u4ef6\u672b\u5c3e\u5f00\u59cb\u5411\u524d\u79fb\u52a8\n    while f.read(1) != b&#39;\\n&#39;:  # \u8bfb\u53d6\u76f4\u5230\u9047\u5230\u6362\u884c\u7b26\n        f.seek(-2, 1)  # \u5411\u524d\u79fb\u52a8\u4e24\u4e2a\u5b57\u8282\n    last_line = f.readline().decode()  # \u8bfb\u53d6\u6700\u540e\u4e00\u884c\nprint(last_line)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u9ad8\u6548\u4e14\u9002\u7528\u4e8e\u5927\u578b\u6587\u4ef6\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u4f7f\u7528\u54ea\u4e9b\u65b9\u6cd5\u5728\u6587\u4ef6\u672b\u5c3e\u6dfb\u52a0\u5185\u5bb9\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>with open(&#39;yourfile.txt&#39;, &#39;a&#39;) as f:<\/code>\u6765\u6253\u5f00\u6587\u4ef6\u5e76\u4ee5\u9644\u52a0\u6a21\u5f0f\u5199\u5165\u6570\u636e\u3002\u8fd9\u79cd\u6a21\u5f0f\u5141\u8bb8\u4f60\u5728\u6587\u4ef6\u672b\u5c3e\u6dfb\u52a0\u5185\u5bb9\uff0c\u800c\u4e0d\u8986\u76d6\u73b0\u6709\u6570\u636e\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;yourfile.txt&#39;, &#39;a&#39;) as f:\n    f.write(&#39;\u8fd9\u662f\u8981\u6dfb\u52a0\u7684\u5185\u5bb9\\n&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\uff0c\u4f60\u5c31\u80fd\u8f7b\u677e\u5730\u5c06\u65b0\u7684\u5185\u5bb9\u6dfb\u52a0\u5230\u6587\u4ef6\u7684\u672b\u5c3e\u3002<\/p>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u4e3a\u7a7a\uff1f<\/strong><br \/>\u8981\u68c0\u67e5\u6587\u4ef6\u662f\u5426\u4e3a\u7a7a\uff0c\u53ef\u4ee5\u4f7f\u7528<code>os<\/code>\u6a21\u5757\u4e2d\u7684<code>stat<\/code>\u65b9\u6cd5\u83b7\u53d6\u6587\u4ef6\u7684\u5927\u5c0f\u3002\u5982\u679c\u6587\u4ef6\u5927\u5c0f\u4e3a0\uff0c\u5219\u8868\u793a\u6587\u4ef6\u4e3a\u7a7a\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import os\n\nfile_path = &#39;yourfile.txt&#39;\nif os.stat(file_path).st_size == 0:\n    print(&quot;\u6587\u4ef6\u4e3a\u7a7a&quot;)\nelse:\n    print(&quot;\u6587\u4ef6\u4e0d\u4e3a\u7a7a&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u4e14\u6709\u6548\uff0c\u9002\u7528\u4e8e\u5404\u79cd\u6587\u4ef6\u7c7b\u578b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u5728\u6587\u4ef6\u6700\u540e\u4e00\u884c\u6dfb\u52a0\u5185\u5bb9\u7684\u65b9\u6cd5\u6709\u591a\u79cd\uff0c\u4e3b\u8981\u5305\u62ec\uff1a\u4f7f\u7528\u8ffd\u52a0\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\u3001\u4f7f\u7528\u5e26\u6709 a \u6a21\u5f0f\u7684 open [&hellip;]","protected":false},"author":3,"featured_media":1095071,"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\/1095057"}],"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=1095057"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095057\/revisions"}],"predecessor-version":[{"id":1095073,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1095057\/revisions\/1095073"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1095071"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1095057"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1095057"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1095057"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}