{"id":960736,"date":"2024-12-27T03:52:21","date_gmt":"2024-12-26T19:52:21","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/960736.html"},"modified":"2024-12-27T03:52:23","modified_gmt":"2024-12-26T19:52:23","slug":"python%e5%a6%82%e4%bd%95%e5%86%99%e5%88%b0text","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/960736.html","title":{"rendered":"python\u5982\u4f55\u5199\u5230text"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25103148\/6b25810f-bdfa-45df-9358-3b3799be9533.webp\" alt=\"python\u5982\u4f55\u5199\u5230text\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5c06\u6570\u636e\u5199\u5165\u6587\u672c\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u3001\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u7ba1\u7406\u6587\u4ef6\u8d44\u6e90\u3001\u9009\u62e9\u9002\u5f53\u7684\u6587\u4ef6\u6a21\u5f0f\uff08\u5982\u5199\u5165&#39;w&#39;\u3001\u8ffd\u52a0&#39;a&#39;\uff09\u7b49\u3002\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u5199\u64cd\u4f5c\u5b8c\u6210\u540e\u88ab\u6b63\u786e\u5173\u95ed\uff0c\u4ece\u800c\u51cf\u5c11\u8d44\u6e90\u6cc4\u6f0f\u7684\u98ce\u9669\u3002\u4ee5\u4e0b\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u5e76\u63d0\u4f9b\u793a\u4f8b\u4ee3\u7801\u3002<\/strong><\/p>\n<\/p>\n<p><p>\u4e00\u3001\u4f7f\u7528<code>open()<\/code>\u51fd\u6570<\/p>\n<\/p>\n<p><p><code>open()<\/code>\u51fd\u6570\u662fPython\u4e2d\u6700\u57fa\u672c\u7684\u6587\u4ef6\u64cd\u4f5c\u51fd\u6570\u4e4b\u4e00\uff0c\u7528\u4e8e\u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u8bfb\u5199\u3002\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u65f6\uff0c\u9700\u8981\u6307\u5b9a\u6587\u4ef6\u540d\u548c\u6a21\u5f0f\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528\u5199\u5165\u6a21\u5f0f<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u5199\u5165\u6a21\u5f0f\u4e0b\uff0c\u6587\u4ef6\u4e2d\u7684\u5185\u5bb9\u5c06\u88ab\u6e05\u7a7a\uff0c\u4efb\u4f55\u65b0\u5199\u5165\u7684\u5185\u5bb9\u5c06\u8986\u76d6\u539f\u6709\u7684\u5185\u5bb9\u3002\u9002\u7528\u4e8e\u9700\u8981\u5b8c\u5168\u66f4\u65b0\u6587\u4ef6\u5185\u5bb9\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u6587\u4ef6\u4ee5\u8fdb\u884c\u5199\u5165<\/p>\n<p>file = open(&#39;example.txt&#39;, &#39;w&#39;)<\/p>\n<h2><strong>\u5199\u5165\u5185\u5bb9<\/strong><\/h2>\n<p>file.write(&#39;Hello, World!\\n&#39;)<\/p>\n<p>file.write(&#39;This is a new line.&#39;)<\/p>\n<h2><strong>\u5173\u95ed\u6587\u4ef6<\/strong><\/h2>\n<p>file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528\u8ffd\u52a0\u6a21\u5f0f<\/strong><\/li>\n<\/ol>\n<p><p>\u5728\u8ffd\u52a0\u6a21\u5f0f\u4e0b\uff0c\u65b0\u7684\u5185\u5bb9\u5c06\u88ab\u6dfb\u52a0\u5230\u6587\u4ef6\u672b\u5c3e\uff0c\u800c\u4e0d\u4f1a\u8986\u76d6\u73b0\u6709\u7684\u5185\u5bb9\u3002\u9002\u7528\u4e8e\u9700\u8981\u5728\u73b0\u6709\u6587\u4ef6\u4e2d\u8ffd\u52a0\u65b0\u5185\u5bb9\u7684\u573a\u666f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u6587\u4ef6\u4ee5\u8fdb\u884c\u8ffd\u52a0<\/p>\n<p>file = open(&#39;example.txt&#39;, &#39;a&#39;)<\/p>\n<h2><strong>\u8ffd\u52a0\u5185\u5bb9<\/strong><\/h2>\n<p>file.write(&#39;\\nThis is an appended line.&#39;)<\/p>\n<h2><strong>\u5173\u95ed\u6587\u4ef6<\/strong><\/h2>\n<p>file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e8c\u3001\u4f7f\u7528<code>with<\/code>\u8bed\u53e5<\/p>\n<\/p>\n<p><p><code>with<\/code>\u8bed\u53e5\u7528\u4e8e\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u8d44\u6e90\uff0c\u786e\u4fdd\u6587\u4ef6\u5728\u4f7f\u7528\u540e\u88ab\u6b63\u786e\u5173\u95ed\u3002\u901a\u8fc7\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\uff0c\u53ef\u4ee5\u907f\u514d\u5fd8\u8bb0\u5173\u95ed\u6587\u4ef6\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u5199\u5165\u6a21\u5f0f\u4e0e<code>with<\/code>\u8bed\u53e5<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528with\u8bed\u53e5\u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u5199\u5165<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    # \u5199\u5165\u5185\u5bb9<\/p>\n<p>    file.write(&#39;Hello, World!\\n&#39;)<\/p>\n<p>    file.write(&#39;This is another new line.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u8ffd\u52a0\u6a21\u5f0f\u4e0e<code>with<\/code>\u8bed\u53e5<\/strong><\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u4f7f\u7528with\u8bed\u53e5\u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u8ffd\u52a0<\/p>\n<p>with open(&#39;example.txt&#39;, &#39;a&#39;) as file:<\/p>\n<p>    # \u8ffd\u52a0\u5185\u5bb9<\/p>\n<p>    file.write(&#39;\\nThis is another appended line.&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e09\u3001\u4f7f\u7528\u5176\u4ed6\u5e93\u8fdb\u884c\u5199\u5165<\/p>\n<\/p>\n<p><p>\u9664\u4e86\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\uff0cPython\u4e2d\u8fd8\u6709\u5176\u4ed6\u5e93\u53ef\u4ee5\u8fdb\u884c\u6587\u4ef6\u5199\u5165\u64cd\u4f5c\uff0c\u5982<code>pandas<\/code>\u548c<code>csv<\/code>\u3002<\/p>\n<\/p>\n<ol>\n<li><strong>\u4f7f\u7528pandas\u5199\u5165\u6570\u636e\u5230CSV\u6587\u4ef6<\/strong><\/li>\n<\/ol>\n<p><p><code>pandas<\/code>\u5e93\u975e\u5e38\u9002\u5408\u7528\u4e8e\u5904\u7406\u548c\u5b58\u50a8\u8868\u683c\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa\u6570\u636e\u6846<\/strong><\/h2>\n<p>data = {&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;, &#39;Charlie&#39;], &#39;Age&#39;: [25, 30, 35]}<\/p>\n<p>df = pd.DataFrame(data)<\/p>\n<h2><strong>\u5199\u5165\u6570\u636e\u5230CSV\u6587\u4ef6<\/strong><\/h2>\n<p>df.to_csv(&#39;people.csv&#39;, index=False)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<ol start=\"2\">\n<li><strong>\u4f7f\u7528csv\u6a21\u5757\u5199\u5165\u6570\u636e<\/strong><\/li>\n<\/ol>\n<p><p><code>csv<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u63a5\u53e3\u7528\u4e8eCSV\u6587\u4ef6\u7684\u8bfb\u5199\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<h2><strong>\u51c6\u5907\u6570\u636e<\/strong><\/h2>\n<p>data = [[&#39;Name&#39;, &#39;Age&#39;], [&#39;Alice&#39;, 25], [&#39;Bob&#39;, 30], [&#39;Charlie&#39;, 35]]<\/p>\n<h2><strong>\u5199\u5165\u6570\u636e\u5230CSV\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;people.csv&#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>\u56db\u3001\u5904\u7406\u5927\u6587\u4ef6<\/p>\n<\/p>\n<p><p>\u5bf9\u4e8e\u9700\u8981\u5904\u7406\u7684\u5927\u578b\u6587\u672c\u6587\u4ef6\uff0c\u9010\u884c\u5199\u5165\u53ef\u80fd\u66f4\u4e3a\u9ad8\u6548\u3002\u53ef\u4ee5\u901a\u8fc7\u5faa\u73af\u6765\u9010\u884c\u5199\u5165\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u51c6\u5907\u5927\u91cf\u6570\u636e<\/p>\n<p>lines = [&#39;This is line {}\\n&#39;.format(i) for i in range(1, 1001)]<\/p>\n<h2><strong>\u9010\u884c\u5199\u5165\u6570\u636e<\/strong><\/h2>\n<p>with open(&#39;largefile.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    for line in lines:<\/p>\n<p>        file.write(line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e94\u3001\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316<\/p>\n<\/p>\n<p><p>\u5728\u5199\u5165\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u6765\u52a8\u6001\u751f\u6210\u5185\u5bb9\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u6587\u4ef6\u8fdb\u884c\u5199\u5165<\/p>\n<p>with open(&#39;formatted.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    # \u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u5199\u5165<\/p>\n<p>    for i in range(1, 6):<\/p>\n<p>        file.write(f&#39;This is formatted line {i}\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u603b\u7ed3\uff1a<\/p>\n<\/p>\n<p><p>\u5728Python\u4e2d\u5199\u5165\u6587\u672c\u6587\u4ef6\u662f\u4e00\u4e2a\u5e38\u89c1\u4e14\u5bb9\u6613\u5b9e\u73b0\u7684\u4efb\u52a1\u3002\u901a\u8fc7\u5408\u7406\u9009\u62e9\u6587\u4ef6\u6a21\u5f0f\uff08\u5199\u5165\u6216\u8ffd\u52a0\uff09\uff0c\u5e76\u5229\u7528<code>with<\/code>\u8bed\u53e5\u786e\u4fdd\u6587\u4ef6\u8d44\u6e90\u7684\u6b63\u786e\u7ba1\u7406\uff0c\u53ef\u4ee5\u6709\u6548\u5730\u8fdb\u884c\u6587\u4ef6\u64cd\u4f5c\u3002\u5bf9\u4e8e\u8868\u683c\u6570\u636e\uff0c<code>pandas<\/code>\u548c<code>csv<\/code>\u6a21\u5757\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u652f\u6301\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u9700\u6c42\u573a\u666f\u3002\u540c\u65f6\uff0c\u5bf9\u4e8e\u5927\u578b\u6587\u4ef6\uff0c\u9010\u884c\u5199\u5165\u53ef\u4ee5\u63d0\u9ad8\u6548\u7387\uff0c\u907f\u514d\u5185\u5b58\u5360\u7528\u8fc7\u591a\u3002\u901a\u8fc7\u7ed3\u5408\u4f7f\u7528\u5b57\u7b26\u4e32\u683c\u5f0f\u5316\u6280\u672f\uff0c\u53ef\u4ee5\u7075\u6d3b\u5730\u751f\u6210\u548c\u5199\u5165\u52a8\u6001\u5185\u5bb9\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u521b\u5efa\u548c\u5199\u5165\u6587\u672c\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u5e76\u5199\u5165\u6587\u672c\u6587\u4ef6\u3002\u4f7f\u7528\u6a21\u5f0f<code>&#39;w&#39;<\/code>\uff08\u5199\u5165\uff09\u6216<code>&#39;a&#39;<\/code>\uff08\u8ffd\u52a0\uff09\u53ef\u4ee5\u6253\u5f00\u6587\u4ef6\u3002\u5728\u6253\u5f00\u6587\u4ef6\u540e\uff0c\u4f7f\u7528<code>write()<\/code>\u65b9\u6cd5\u5c06\u5185\u5bb9\u5199\u5165\u6587\u4ef6\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;w&#39;) as file:\n    file.write(&#39;Hello, World!&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u5728\u5f53\u524d\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>example.txt<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u5199\u5165\u201cHello, World!\u201d\u3002<\/p>\n<p><strong>\u4f7f\u7528Python\u5199\u5165\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u5982\u4f55\u5904\u7406\u7f16\u7801\u95ee\u9898\uff1f<\/strong><br \/>\u5728\u5199\u5165\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u7279\u522b\u662f\u5305\u542b\u975eASCII\u5b57\u7b26\u7684\u5185\u5bb9\uff0c\u6307\u5b9a\u7f16\u7801\u975e\u5e38\u91cd\u8981\u3002\u53ef\u4ee5\u5728<code>open()<\/code>\u51fd\u6570\u4e2d\u4f7f\u7528<code>encoding<\/code>\u53c2\u6570\u6765\u786e\u4fdd\u6b63\u786e\u5199\u5165\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;example.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as file:\n    file.write(&#39;\u4f60\u597d\uff0c\u4e16\u754c\uff01&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u5c06\u521b\u5efa\u4e00\u4e2aUTF-8\u7f16\u7801\u7684\u6587\u672c\u6587\u4ef6\uff0c\u786e\u4fdd\u4e2d\u6587\u5b57\u7b26\u6b63\u786e\u5b58\u50a8\u3002<\/p>\n<p><strong>\u5982\u679c\u9700\u8981\u5728Python\u4e2d\u5199\u5165\u591a\u884c\u6587\u672c\uff0c\u5e94\u8be5\u5982\u4f55\u64cd\u4f5c\uff1f<\/strong><br \/>\u4e3a\u4e86\u5199\u5165\u591a\u884c\u6587\u672c\uff0c\u53ef\u4ee5\u4f7f\u7528<code>writelines()<\/code>\u65b9\u6cd5\u6216\u5728<code>write()<\/code>\u65b9\u6cd5\u4e2d\u6dfb\u52a0\u6362\u884c\u7b26<code>\\n<\/code>\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">lines = [&#39;\u7b2c\u4e00\u884c\\n&#39;, &#39;\u7b2c\u4e8c\u884c\\n&#39;, &#39;\u7b2c\u4e09\u884c\\n&#39;]\nwith open(&#39;example.txt&#39;, &#39;w&#39;) as file:\n    file.writelines(lines)\n<\/code><\/pre>\n<p>\u6b64\u4ee3\u7801\u5c06\u521b\u5efa\u4e00\u4e2a\u5305\u542b\u4e09\u884c\u6587\u672c\u7684\u6587\u4ef6\uff0c\u6bcf\u884c\u4e4b\u95f4\u4ee5\u6362\u884c\u7b26\u5206\u9694\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u591a\u79cd\u65b9\u6cd5\u5c06\u6570\u636e\u5199\u5165\u6587\u672c\u6587\u4ef6\uff0c\u5176\u4e2d\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684open()\u51fd\u6570\u3001\u4f7f\u7528with\u8bed\u53e5\u7ba1\u7406 [&hellip;]","protected":false},"author":3,"featured_media":960738,"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\/960736"}],"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=960736"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/960736\/revisions"}],"predecessor-version":[{"id":960739,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/960736\/revisions\/960739"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/960738"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=960736"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=960736"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=960736"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}