{"id":1071507,"date":"2025-01-08T11:07:54","date_gmt":"2025-01-08T03:07:54","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1071507.html"},"modified":"2025-01-08T11:07:56","modified_gmt":"2025-01-08T03:07:56","slug":"python%e5%a6%82%e4%bd%95%e6%8a%8a%e8%be%93%e5%87%ba%e7%bb%93%e6%9e%9c%e5%ad%98%e5%85%a5txt%e6%96%87%e4%bb%b6-2","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1071507.html","title":{"rendered":"python\u5982\u4f55\u628a\u8f93\u51fa\u7ed3\u679c\u5b58\u5165txt\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25102010\/5354bcc8-e987-4072-ac9d-b11ee94dde07.webp\" alt=\"python\u5982\u4f55\u628a\u8f93\u51fa\u7ed3\u679c\u5b58\u5165txt\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u5f0f\u5c06\u8f93\u51fa\u7ed3\u679c\u5b58\u5165txt\u6587\u4ef6\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\u3001\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u3001\u4ee5\u53ca\u9ad8\u7ea7\u6a21\u5757\uff0c\u5982csv\u3001json\u7b49\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a\u4f7f\u7528\u5185\u7f6e\u7684open\u51fd\u6570\u548cwrite\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5c06\u8f93\u51fa\u7ed3\u679c\u5199\u5165txt\u6587\u4ef6\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u5c06Python\u7684\u8f93\u51fa\u7ed3\u679c\u5b58\u5165txt\u6587\u4ef6\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u51e0\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528\u5185\u7f6e\u7684open\u51fd\u6570\u548cwrite\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p>Python\u7684\u5185\u7f6e\u51fd\u6570<code>open<\/code>\u53ef\u4ee5\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c<code>write<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u5185\u5bb9\u5199\u5165\u6587\u4ef6\u3002\u5177\u4f53\u6b65\u9aa4\u5982\u4e0b\uff1a<\/p>\n<\/p>\n<ol>\n<li><strong>\u6253\u5f00\u6587\u4ef6<\/strong>\uff1a\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u6253\u5f00\u6587\u4ef6\uff0c\u6a21\u5f0f\u8bbe\u4e3a<code>w<\/code>\uff08\u5199\uff09\u6216<code>a<\/code>\uff08\u8ffd\u52a0\uff09\u3002<\/li>\n<li><strong>\u5199\u5165\u5185\u5bb9<\/strong>\uff1a\u4f7f\u7528\u6587\u4ef6\u5bf9\u8c61\u7684<code>write<\/code>\u65b9\u6cd5\u5c06\u5185\u5bb9\u5199\u5165\u6587\u4ef6\u3002<\/li>\n<li><strong>\u5173\u95ed\u6587\u4ef6<\/strong>\uff1a\u4f7f\u7528\u6587\u4ef6\u5bf9\u8c61\u7684<code>close<\/code>\u65b9\u6cd5\u5173\u95ed\u6587\u4ef6\uff0c\u4ee5\u786e\u4fdd\u6240\u6709\u5185\u5bb9\u88ab\u5199\u5165\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># Example of writing to a file<\/p>\n<p>output = &quot;This is the output text that will be written to the file.&quot;<\/p>\n<p>with open(&quot;output.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u7b80\u5355\u76f4\u89c2\uff0c\u975e\u5e38\u9002\u5408\u5199\u5165\u5c11\u91cf\u7684\u6587\u672c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528print\u51fd\u6570\u5e76\u91cd\u5b9a\u5411\u8f93\u51fa<\/h3>\n<\/p>\n<p><p>\u6709\u65f6\u60a8\u53ef\u80fd\u5e0c\u671b\u5c06<code>print<\/code>\u51fd\u6570\u7684\u8f93\u51fa\u76f4\u63a5\u5199\u5165\u6587\u4ef6\u3002\u53ef\u4ee5\u4f7f\u7528<code>sys.stdout<\/code>\u8fdb\u884c\u91cd\u5b9a\u5411\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>output = &quot;This is the output text that will be written to the file.&quot;<\/p>\n<p>with open(&quot;output.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    sys.stdout = file<\/p>\n<p>    print(output)<\/p>\n<p>    sys.stdout = sys.__stdout__  # Reset to default stdout<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668<\/h3>\n<\/p>\n<p><p>\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff08\u5373<code>with<\/code>\u8bed\u53e5\uff09\u53ef\u4ee5\u81ea\u52a8\u5904\u7406\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed\uff0c\u786e\u4fdd\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u6587\u4ef6\u88ab\u6b63\u786e\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">output = &quot;This is the output text that will be written to the file.&quot;<\/p>\n<p>with open(&quot;output.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(output)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u7b80\u6d01\uff0c\u800c\u4e14\u66f4\u5b89\u5168\uff0c\u56e0\u4e3a\u5373\u4f7f\u5728\u5199\u5165\u8fc7\u7a0b\u4e2d\u53d1\u751f\u5f02\u5e38\uff0c\u6587\u4ef6\u4e5f\u4f1a\u88ab\u6b63\u786e\u5173\u95ed\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u5c06Python\u6570\u636e\u7ed3\u6784\u5199\u5165txt\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff08\u5982\u5217\u8868\u3001\u5b57\u5178\uff09\uff0c\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u540e\u518d\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [&quot;Line 1&quot;, &quot;Line 2&quot;, &quot;Line 3&quot;]<\/p>\n<p>with open(&quot;output.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    for item in data:<\/p>\n<p>        file.write(f&quot;{item}\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u975e\u5e38\u9002\u5408\u5199\u5165\u591a\u884c\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u4f7f\u7528\u9ad8\u7ea7\u6a21\u5757\uff08\u5982csv\u3001json\uff09<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u7279\u5b9a\u683c\u5f0f\u7684\u6570\u636e\uff0cPython\u63d0\u4f9b\u4e86\u9ad8\u7ea7\u6a21\u5757\u6765\u7b80\u5316\u64cd\u4f5c\u3002\u4f8b\u5982\uff0c<code>csv<\/code>\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u5904\u7406CSV\u683c\u5f0f\u7684\u6570\u636e\uff0c<code>json<\/code>\u6a21\u5757\u53ef\u4ee5\u8f7b\u677e\u5904\u7406JSON\u683c\u5f0f\u7684\u6570\u636e\u3002<\/p>\n<\/p>\n<p><h4>\u4f7f\u7528csv\u6a21\u5757\u5199\u5165CSV\u6587\u4ef6\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>data = [[&quot;Name&quot;, &quot;Age&quot;], [&quot;Alice&quot;, 30], [&quot;Bob&quot;, 25]]<\/p>\n<p>with open(&quot;output.csv&quot;, &quot;w&quot;, 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><h4>\u4f7f\u7528json\u6a21\u5757\u5199\u5165JSON\u6587\u4ef6\uff1a<\/h4>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30}<\/p>\n<p>with open(&quot;output.json&quot;, &quot;w&quot;) as file:<\/p>\n<p>    json.dump(data, file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u5904\u7406\u5927\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u6587\u4ef6\u7684\u5199\u5165\uff0c\u53ef\u4ee5\u8003\u8651\u4f7f\u7528\u751f\u6210\u5668\u6216\u6279\u91cf\u5199\u5165\u7684\u65b9\u5f0f\uff0c\u4ee5\u63d0\u9ad8\u5199\u5165\u6548\u7387\u548c\u8282\u7701\u5185\u5b58\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">def generate_data():<\/p>\n<p>    for i in range(1000000):<\/p>\n<p>        yield f&quot;This is line {i}\\n&quot;<\/p>\n<p>with open(&quot;output.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    for line in generate_data():<\/p>\n<p>        file.write(line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8fd9\u79cd\u65b9\u6cd5\u53ef\u4ee5\u6709\u6548\u5904\u7406\u5927\u6570\u636e\u91cf\u7684\u5199\u5165\u4efb\u52a1\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u9644\u52a0\u64cd\u4f5c\uff1a\u8bfb\u53d6\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5199\u5165\u6587\u4ef6\u540e\uff0c\u60a8\u53ef\u80fd\u5e0c\u671b\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u4ee5\u786e\u8ba4\u5199\u5165\u662f\u5426\u6210\u529f\u3002\u53ef\u4ee5\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u7684<code>r<\/code>\uff08\u8bfb\uff09\u6a21\u5f0f\u6765\u5b9e\u73b0\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;output.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    print(content)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u65b9\u6cd5\uff0c\u60a8\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06Python\u7684\u8f93\u51fa\u7ed3\u679c\u5b58\u5165txt\u6587\u4ef6\uff0c\u5e76\u6839\u636e\u9700\u8981\u4f7f\u7528\u4e0d\u540c\u7684\u6280\u672f\u548c\u6a21\u5757\u6765\u5904\u7406\u5404\u79cd\u573a\u666f\u3002\u4ece\u7b80\u5355\u7684\u6587\u672c\u5199\u5165\u5230\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\u5904\u7406\uff0cPython\u63d0\u4f9b\u4e86\u4e00\u6574\u5957\u529f\u80fd\u5f3a\u5927\u7684\u5de5\u5177\uff0c\u5e2e\u52a9\u60a8\u9ad8\u6548\u5b8c\u6210\u6587\u4ef6\u64cd\u4f5c\u4efb\u52a1\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u5e76\u5199\u5165\u4e00\u4e2aTXT\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u521b\u5efa\u4e00\u4e2aTXT\u6587\u4ef6\u5e76\u5199\u5165\u6570\u636e\u3002\u4f7f\u7528<code>&#39;w&#39;<\/code>\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\u53ef\u4ee5\u6e05\u7a7a\u6587\u4ef6\u5185\u5bb9\u5e76\u5199\u5165\u65b0\u5185\u5bb9\uff0c\u800c\u4f7f\u7528<code>&#39;a&#39;<\/code>\u6a21\u5f0f\u53ef\u4ee5\u5728\u6587\u4ef6\u672b\u5c3e\u8ffd\u52a0\u5185\u5bb9\u3002\u793a\u4f8b\u5982\u4e0b\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&#39;output.txt&#39;, &#39;w&#39;) as f:\n    f.write(&#39;\u8fd9\u662f\u8981\u5199\u5165\u6587\u4ef6\u7684\u5185\u5bb9\u3002&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>output.txt<\/code>\u7684\u6587\u4ef6\u5e76\u5199\u5165\u6307\u5b9a\u7684\u6587\u672c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06Python\u7684\u8f93\u51fa\u7ed3\u679c\u5199\u5165TXT\u6587\u4ef6\u800c\u4e0d\u5f71\u54cd\u63a7\u5236\u53f0\u8f93\u51fa\uff1f<\/strong><br \/>\u53ef\u4ee5\u901a\u8fc7\u91cd\u5b9a\u5411\u8f93\u51fa\u6d41\u6765\u5b9e\u73b0\u5c06\u7ed3\u679c\u540c\u65f6\u8f93\u51fa\u5230\u63a7\u5236\u53f0\u548cTXT\u6587\u4ef6\u3002\u4f7f\u7528<code>sys.stdout<\/code>\u53ef\u4ee5\u5c06\u8f93\u51fa\u6d41\u91cd\u5b9a\u5411\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">import sys\n\nwith open(&#39;output.txt&#39;, &#39;w&#39;) as f:\n    sys.stdout = f\n    print(&#39;\u8fd9\u6761\u4fe1\u606f\u4f1a\u5199\u5165\u6587\u4ef6&#39;)\n    sys.stdout = sys.__stdout__\nprint(&#39;\u8fd9\u6761\u4fe1\u606f\u4f1a\u663e\u793a\u5728\u63a7\u5236\u53f0&#39;)\n<\/code><\/pre>\n<p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c\u7b2c\u4e00\u6761\u4fe1\u606f\u5c06\u88ab\u5199\u5165\u6587\u4ef6\uff0c\u800c\u7b2c\u4e8c\u6761\u4fe1\u606f\u5219\u4f1a\u5728\u63a7\u5236\u53f0\u663e\u793a\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u8bfb\u53d6TXT\u6587\u4ef6\u7684\u5185\u5bb9\uff1f<\/strong><br \/>\u82e5\u8981\u8bfb\u53d6\u4e4b\u524d\u5199\u5165\u7684TXT\u6587\u4ef6\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u4ee5<code>&#39;r&#39;<\/code>\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\u3002\u4f7f\u7528<code>read()<\/code>\u6216<code>readlines()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u83b7\u53d6\u6587\u4ef6\u4e2d\u7684\u6570\u636e\u3002\u4ee5\u4e0b\u662f\u8bfb\u53d6\u5185\u5bb9\u7684\u793a\u4f8b\uff1a<\/p>\n<pre><code class=\"language-python\">with open(&#39;output.txt&#39;, &#39;r&#39;) as f:\n    content = f.read()\n    print(content)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u6253\u5f00<code>output.txt<\/code>\u6587\u4ef6\u5e76\u6253\u5370\u5176\u5185\u5bb9\u5230\u63a7\u5236\u53f0\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u51e0\u79cd\u65b9\u5f0f\u5c06\u8f93\u51fa\u7ed3\u679c\u5b58\u5165txt\u6587\u4ef6\uff0c\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\u3001\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u3001\u4ee5\u53ca\u9ad8\u7ea7\u6a21\u5757\uff0c [&hellip;]","protected":false},"author":3,"featured_media":1071518,"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\/1071507"}],"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=1071507"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1071507\/revisions"}],"predecessor-version":[{"id":1071520,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1071507\/revisions\/1071520"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1071518"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1071507"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1071507"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1071507"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}