{"id":1135265,"date":"2025-01-08T21:23:31","date_gmt":"2025-01-08T13:23:31","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1135265.html"},"modified":"2025-01-08T21:23:34","modified_gmt":"2025-01-08T13:23:34","slug":"python%e5%a6%82%e4%bd%95%e8%be%93%e5%87%ba%e4%b8%80%e4%b8%aa%e6%96%87%e6%9c%ac%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1135265.html","title":{"rendered":"python\u5982\u4f55\u8f93\u51fa\u4e00\u4e2a\u6587\u672c\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25104001\/c78b7a67-c978-4780-ae71-c7eab26b83c4.webp\" alt=\"python\u5982\u4f55\u8f93\u51fa\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>\u4f7f\u7528Python\u8f93\u51fa\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u3001\u7b2c\u4e09\u65b9\u5e93\u5982<code>pandas<\/code>\u3001\u4ee5\u53ca\u9ad8\u7ea7\u7684\u6587\u4ef6\u5904\u7406\u65b9\u5f0f\u3002<\/strong> \u6700\u5e38\u89c1\u4e14\u63a8\u8350\u7684\u65b9\u6cd5\u662f\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\uff0c\u56e0\u4e3a\u5b83\u7b80\u5355\u3001\u76f4\u89c2\u4e14\u529f\u80fd\u5f3a\u5927\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u8f93\u51fa\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u8f93\u51fa\u6587\u672c\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>Python\u7684\u5185\u7f6e<code>open()<\/code>\u51fd\u6570\u63d0\u4f9b\u4e86\u4e00\u4e2a\u7b80\u5355\u800c\u5f3a\u5927\u7684\u65b9\u5f0f\u6765\u5904\u7406\u6587\u4ef6\u64cd\u4f5c\u3002\u4ee5\u4e0b\u662f\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u8f93\u51fa\u6587\u672c\u6587\u4ef6\u7684\u57fa\u672c\u6b65\u9aa4\uff1a<\/p>\n<\/p>\n<ol>\n<li>\u6253\u5f00\u6587\u4ef6\uff1a\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u5e76\u6307\u5b9a\u6587\u4ef6\u540d\u548c\u6a21\u5f0f\u3002<\/li>\n<li>\u5199\u5165\u5185\u5bb9\uff1a\u4f7f\u7528\u6587\u4ef6\u5bf9\u8c61\u7684<code>write()<\/code>\u65b9\u6cd5\u3002<\/li>\n<li>\u5173\u95ed\u6587\u4ef6\uff1a\u4f7f\u7528\u6587\u4ef6\u5bf9\u8c61\u7684<code>close()<\/code>\u65b9\u6cd5\u4ee5\u786e\u4fdd\u8d44\u6e90\u91ca\u653e\u3002<\/li>\n<\/ol>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u6587\u4ef6\uff08\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5219\u4f1a\u81ea\u52a8\u521b\u5efa\uff09<\/p>\n<p>file = open(&quot;example.txt&quot;, &quot;w&quot;)<\/p>\n<h2><strong>\u5199\u5165\u5185\u5bb9<\/strong><\/h2>\n<p>file.write(&quot;Hello, world!\\n&quot;)<\/p>\n<p>file.write(&quot;This is a new line of text.&quot;)<\/p>\n<h2><strong>\u5173\u95ed\u6587\u4ef6<\/strong><\/h2>\n<p>file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e8c\u3001\u4ee5\u8ffd\u52a0\u6a21\u5f0f\u5199\u5165\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u9664\u4e86\u8986\u76d6\u6587\u4ef6\u5185\u5bb9\u5916\uff0c\u8fd8\u53ef\u4ee5\u4f7f\u7528\u8ffd\u52a0\u6a21\u5f0f\uff08<code>&#39;a&#39;<\/code>\uff09\u5c06\u65b0\u5185\u5bb9\u6dfb\u52a0\u5230\u6587\u4ef6\u672b\u5c3e\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4ee5\u8ffd\u52a0\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6<\/p>\n<p>file = open(&quot;example.txt&quot;, &quot;a&quot;)<\/p>\n<h2><strong>\u5199\u5165\u65b0\u5185\u5bb9<\/strong><\/h2>\n<p>file.write(&quot;\\nThis is an appended line of text.&quot;)<\/p>\n<h2><strong>\u5173\u95ed\u6587\u4ef6<\/strong><\/h2>\n<p>file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u7ba1\u7406\u6587\u4ef6\u4e0a\u4e0b\u6587<\/h3>\n<\/p>\n<p><p>Python\u7684<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed\uff0c\u786e\u4fdd\u5373\u4f7f\u53d1\u751f\u5f02\u5e38\u4e5f\u80fd\u6b63\u786e\u5173\u95ed\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528with\u8bed\u53e5\u6253\u5f00\u6587\u4ef6<\/p>\n<p>with open(&quot;example.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(&quot;Hello, world!\\n&quot;)<\/p>\n<p>    file.write(&quot;This is a new line of text.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5199\u5165\u591a\u884c\u6587\u672c<\/h3>\n<\/p>\n<p><p>\u53ef\u4ee5\u4f7f\u7528<code>writelines()<\/code>\u65b9\u6cd5\u5c06\u4e00\u4e2a\u5217\u8868\u4e2d\u7684\u591a\u884c\u6587\u672c\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">lines = [<\/p>\n<p>    &quot;First line of text.\\n&quot;,<\/p>\n<p>    &quot;Second line of text.\\n&quot;,<\/p>\n<p>    &quot;Third line of text.\\n&quot;<\/p>\n<p>]<\/p>\n<p>with open(&quot;example.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.writelines(lines)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u7b2c\u4e09\u65b9\u5e93\u5982<code>pandas<\/code>\u8f93\u51fa\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u590d\u6742\u7684\u6570\u636e\u5904\u7406\uff0c\u53ef\u4ee5\u4f7f\u7528<code>pandas<\/code>\u5e93\u6765\u8f93\u51fa\u6587\u4ef6\uff0c\u7279\u522b\u662f\u5904\u7406\u8868\u683c\u6570\u636e\u65f6\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u4e00\u4e2aDataFrame<\/strong><\/h2>\n<p>data = {<\/p>\n<p>    &#39;Name&#39;: [&#39;John&#39;, &#39;Anna&#39;, &#39;Peter&#39;],<\/p>\n<p>    &#39;Age&#39;: [28, 24, 35]<\/p>\n<p>}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u8f93\u51fa\u4e3aCSV\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_csv(&quot;output.csv&quot;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u5927\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u5206\u5757\u8bfb\u5199\u4ee5\u8282\u7701\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">chunk_size = 1024  # \u6bcf\u6b21\u8bfb\u53d6\u7684\u5b57\u8282\u6570<\/p>\n<p>with open(&quot;large_file.txt&quot;, &quot;r&quot;) as source_file:<\/p>\n<p>    with open(&quot;output_file.txt&quot;, &quot;w&quot;) as dest_file:<\/p>\n<p>        while chunk := source_file.read(chunk_size):<\/p>\n<p>            dest_file.write(chunk)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757<\/h3>\n<\/p>\n<p><p><code>pathlib<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u8def\u5f84\u64cd\u4f5c\uff0c\u9002\u7528\u4e8ePython 3.4\u53ca\u4ee5\u4e0a\u7248\u672c\u3002<\/p>\n<\/p>\n<p><h4>\u793a\u4f8b\u4ee3\u7801<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">from pathlib import Path<\/p>\n<h2><strong>\u521b\u5efaPath\u5bf9\u8c61<\/strong><\/h2>\n<p>file_path = Path(&quot;example.txt&quot;)<\/p>\n<h2><strong>\u5199\u5165\u5185\u5bb9<\/strong><\/h2>\n<p>file_path.write_text(&quot;Hello, world!\\nThis is a new line of text.&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>\u5728Python\u4e2d\u8f93\u51fa\u6587\u672c\u6587\u4ef6\u7684\u65b9\u6cd5\u591a\u79cd\u591a\u6837\uff0c<strong>\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u662f\u6700\u5e38\u89c1\u4e14\u63a8\u8350\u7684\u65b9\u5f0f<\/strong>\uff0c\u5b83\u7b80\u5355\u3001\u76f4\u89c2\u4e14\u529f\u80fd\u5f3a\u5927\u3002\u5bf9\u4e8e\u7279\u5b9a\u9700\u6c42\uff0c\u53ef\u4ee5\u9009\u62e9\u5176\u4ed6\u65b9\u6cd5\u5982\u4f7f\u7528<code>pandas<\/code>\u5e93\u5904\u7406\u8868\u683c\u6570\u636e\uff0c\u6216\u4f7f\u7528<code>pathlib<\/code>\u6a21\u5757\u8fdb\u884c\u9762\u5411\u5bf9\u8c61\u7684\u6587\u4ef6\u64cd\u4f5c\u3002\u65e0\u8bba\u9009\u62e9\u54ea\u79cd\u65b9\u6cd5\uff0c\u786e\u4fdd\u6b63\u786e\u7ba1\u7406\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed\u662f\u5173\u952e\uff0c\u4ee5\u907f\u514d\u8d44\u6e90\u6cc4\u6f0f\u548c\u6570\u636e\u635f\u574f\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u8bfb\u53d6\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6765\u8bfb\u53d6\u6587\u672c\u6587\u4ef6\u3002\u901a\u8fc7\u6307\u5b9a\u6587\u4ef6\u8def\u5f84\u548c\u6a21\u5f0f\uff08\u4f8b\u5982<code>&#39;r&#39;<\/code>\u8868\u793a\u8bfb\u53d6\uff09\uff0c\u7136\u540e\u4f7f\u7528<code>read()<\/code>\u6216<code>readlines()<\/code>\u65b9\u6cd5\u6765\u83b7\u53d6\u6587\u4ef6\u5185\u5bb9\u3002\u793a\u4f8b\u4ee3\u7801\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;r&#39;) as file:\n    content = file.read()\nprint(content)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u5c31\u53ef\u4ee5\u5c06\u6574\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06\u6570\u636e\u5199\u5165\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\uff1f<\/strong><br \/>\u82e5\u60f3\u5c06\u6570\u636e\u5199\u5165\u6587\u672c\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u5e76\u8bbe\u7f6e\u6a21\u5f0f\u4e3a<code>&#39;w&#39;<\/code>\uff08\u5199\u5165\uff09\u6216<code>&#39;a&#39;<\/code>\uff08\u8ffd\u52a0\uff09\u3002\u4f7f\u7528<code>write()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5b57\u7b26\u4e32\u5199\u5165\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;output.txt&#39;, &#39;w&#39;) as file:\n    file.write(&quot;Hello, World!\\n&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u4f1a\u5728<code>output.txt<\/code>\u4e2d\u5199\u5165&quot;Hello, World!&quot;\u3002<\/p>\n<p><strong>\u5982\u4f55\u5904\u7406\u6587\u4ef6\u8bfb\u53d6\u548c\u5199\u5165\u4e2d\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u65f6\uff0c\u53ef\u80fd\u4f1a\u9047\u5230\u5404\u79cd\u9519\u8bef\uff0c\u4f8b\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u6216\u6743\u9650\u4e0d\u8db3\u3002\u4e3a\u4e86\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u6765\u6355\u6349\u9519\u8bef\u5e76\u8fdb\u884c\u9002\u5f53\u7684\u5904\u7406\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">try:\n    with open(&#39;example.txt&#39;, &#39;r&#39;) as file:\n        content = file.read()\nexcept FileNotFoundError:\n    print(&quot;\u6587\u4ef6\u672a\u627e\u5230\uff0c\u8bf7\u68c0\u67e5\u6587\u4ef6\u8def\u5f84\u3002&quot;)\nexcept PermissionError:\n    print(&quot;\u6743\u9650\u4e0d\u8db3\uff0c\u65e0\u6cd5\u8bfb\u53d6\u6587\u4ef6\u3002&quot;)\n<\/code><\/pre>\n<p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u66f4\u5b89\u5168\u5730\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u4f7f\u7528Python\u8f93\u51fa\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\u7684\u65b9\u6cd5\u6709\u5f88\u591a\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684open()\u51fd\u6570\u3001\u7b2c\u4e09\u65b9\u5e93\u5982pandas\u3001\u4ee5\u53ca\u9ad8\u7ea7 [&hellip;]","protected":false},"author":3,"featured_media":1135269,"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\/1135265"}],"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=1135265"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1135265\/revisions"}],"predecessor-version":[{"id":1135271,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1135265\/revisions\/1135271"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1135269"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1135265"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1135265"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1135265"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}