{"id":1117611,"date":"2025-01-08T18:27:38","date_gmt":"2025-01-08T10:27:38","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1117611.html"},"modified":"2025-01-08T18:27:40","modified_gmt":"2025-01-08T10:27:40","slug":"python%e5%a6%82%e4%bd%95%e6%8a%8a%e4%b8%80%e4%b8%aa%e5%88%97%e8%a1%a8%e5%86%99%e5%88%b0txt","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1117611.html","title":{"rendered":"python\u5982\u4f55\u628a\u4e00\u4e2a\u5217\u8868\u5199\u5230txt"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25081452\/ecc8f685-ae7b-4fda-a1bd-f5ef5e59bd0f.webp\" alt=\"python\u5982\u4f55\u628a\u4e00\u4e2a\u5217\u8868\u5199\u5230txt\" \/><\/p>\n<p><p> <strong>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5c06\u5217\u8868\u5199\u5165\u4e00\u4e2atxt\u6587\u4ef6\uff0c\u5982\u4f7f\u7528<code>with open<\/code>\u3001<code>writelines<\/code>\u65b9\u6cd5\u3001<code>json<\/code>\u5e93\u7b49<\/strong>\uff0c<strong>\u5176\u4e2d<code>with open<\/code>\u65b9\u5f0f\u6700\u4e3a\u5e38\u7528<\/strong>\u3002\u4e0b\u9762\u8be6\u7ec6\u63cf\u8ff0\u5982\u4f55\u4f7f\u7528\u8fd9\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>with open<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>with open<\/code>\u65b9\u6cd5\u662fPython\u4e2d\u5e38\u7528\u7684\u6587\u4ef6\u64cd\u4f5c\u65b9\u6cd5\uff0c\u5b83\u786e\u4fdd\u6587\u4ef6\u5728\u4f7f\u7528\u540e\u81ea\u52a8\u5173\u95ed\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u5217\u8868\u5199\u5165txt\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    for item in my_list:<\/p>\n<p>        file.write(&quot;%s\\n&quot; % item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u6211\u4eec\u9996\u5148\u5b9a\u4e49\u4e86\u4e00\u4e2a\u5305\u542b\u5b57\u7b26\u4e32\u7684\u5217\u8868<code>my_list<\/code>\uff0c\u7136\u540e\u4f7f\u7528<code>with open<\/code>\u6253\u5f00\u4e86\u4e00\u4e2a\u540d\u4e3a<code>output.txt<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u8bbe\u7f6e\u6a21\u5f0f\u4e3a\u5199\u5165\u6a21\u5f0f\uff08&#39;w&#39;\uff09\u3002\u5728<code>with<\/code>\u5757\u4e2d\uff0c\u901a\u8fc7\u5faa\u73af\u904d\u5386\u5217\u8868\u7684\u6bcf\u4e00\u9879\uff0c\u5e76\u5c06\u5176\u9010\u884c\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e8c\u3001\u4f7f\u7528<code>writelines<\/code>\u65b9\u6cd5<\/h3>\n<\/p>\n<p><p><code>writelines<\/code>\u65b9\u6cd5\u53ef\u4ee5\u5c06\u4e00\u4e2a\u5e8f\u5217\u76f4\u63a5\u5199\u5165\u6587\u4ef6\u3002\u6b64\u65b9\u6cd5\u9002\u5408\u4e8e\u9700\u8981\u5c06\u6bcf\u4e2a\u5217\u8868\u9879\u4f5c\u4e3a\u4e00\u884c\u5199\u5165\u6587\u4ef6\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [&#39;apple\\n&#39;, &#39;banana\\n&#39;, &#39;cherry\\n&#39;]<\/p>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.writelines(my_list)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u786e\u4fdd\u6bcf\u4e2a\u5217\u8868\u9879\u90fd\u5305\u542b\u6362\u884c\u7b26\uff0c\u4ee5\u4fbf\u5728\u6587\u4ef6\u4e2d\u9010\u884c\u5199\u5165\u3002<\/p>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>json<\/code>\u5e93<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5217\u8868\u5305\u542b\u590d\u6742\u6570\u636e\u7ed3\u6784\uff0c\u5982\u5d4c\u5957\u5217\u8868\u6216\u5b57\u5178\uff0c\u53ef\u4ee5\u4f7f\u7528<code>json<\/code>\u5e93\u5c06\u5176\u5e8f\u5217\u5316\u5e76\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    json.dump(my_list, file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u6570\u636e\u5c06\u4ee5JSON\u683c\u5f0f\u5199\u5165\u6587\u4ef6\uff0c\u8fd9\u4f7f\u5f97\u5728\u9700\u8981\u65f6\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u6570\u636e\u8bfb\u53d6\u56de\u6765\u5e76\u53cd\u5e8f\u5217\u5316\u3002<\/p>\n<\/p>\n<p><h3>\u56db\u3001\u4f7f\u7528<code>pickle<\/code>\u5e93<\/h3>\n<\/p>\n<p><p><code>pickle<\/code>\u5e93\u4e5f\u53ef\u4ee5\u7528\u6765\u5c06\u5217\u8868\u5e8f\u5217\u5316\u5e76\u5199\u5165\u6587\u4ef6\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u9700\u8981\u4fdd\u5b58\u548c\u8bfb\u53d6Python\u5bf9\u8c61\u7684\u60c5\u51b5\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pickle<\/p>\n<p>my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>with open(&#39;output.txt&#39;, &#39;wb&#39;) as file:<\/p>\n<p>    pickle.dump(my_list, file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>wb<\/code>\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\u4ee5\u8fdb\u884c\u4e8c\u8fdb\u5236\u5199\u5165\uff0c\u5e76\u4f7f\u7528<code>pickle.dump<\/code>\u65b9\u6cd5\u5c06\u5217\u8868\u5e8f\u5217\u5316\u5e76\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e94\u3001\u5c06\u5217\u8868\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5e76\u5199\u5165<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5217\u8868\u5305\u542b\u7b80\u5355\u7684\u6570\u636e\u7c7b\u578b\uff0c\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5e76\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [&#39;apple&#39;, &#39;banana&#39;, &#39;cherry&#39;]<\/p>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;\\n&#39;.join(my_list))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>join<\/code>\u65b9\u6cd5\u5c06\u5217\u8868\u9879\u8fde\u63a5\u4e3a\u4e00\u4e2a\u5305\u542b\u6362\u884c\u7b26\u7684\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u5176\u4e00\u6b21\u6027\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u516d\u3001\u5199\u5165\u5305\u542b\u6570\u5b57\u7684\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5217\u8868\u5305\u542b\u6570\u5b57\uff0c\u53ef\u4ee5\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5e76\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [1, 2, 3, 4, 5]<\/p>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    for item in my_list:<\/p>\n<p>        file.write(&quot;%s\\n&quot; % item)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u5c06\u6bcf\u4e2a\u6570\u5b57\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5e76\u9010\u884c\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h3>\u4e03\u3001\u5199\u5165\u5d4c\u5957\u5217\u8868<\/h3>\n<\/p>\n<p><p>\u5982\u679c\u5217\u8868\u5305\u542b\u5d4c\u5957\u5217\u8868\uff0c\u53ef\u4ee5\u904d\u5386\u6bcf\u4e2a\u5b50\u5217\u8868\u5e76\u5c06\u5176\u9010\u884c\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">my_list = [[&#39;apple&#39;, &#39;banana&#39;], [&#39;cherry&#39;, &#39;date&#39;]]<\/p>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    for sublist in my_list:<\/p>\n<p>        file.write(&quot;%s\\n&quot; % &#39;,&#39;.join(sublist))<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c\u6211\u4eec\u4f7f\u7528<code>join<\/code>\u65b9\u6cd5\u5c06\u6bcf\u4e2a\u5b50\u5217\u8868\u8fde\u63a5\u4e3a\u4e00\u4e2a\u5b57\u7b26\u4e32\uff0c\u5e76\u5c06\u5176\u9010\u884c\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><p>\u901a\u8fc7\u4ee5\u4e0a\u51e0\u79cd\u65b9\u6cd5\uff0c\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06\u5217\u8868\u5199\u5165txt\u6587\u4ef6\u3002\u6839\u636e\u5177\u4f53\u9700\u6c42\uff0c\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u4ee5\u786e\u4fdd\u6570\u636e\u6b63\u786e\u3001\u6709\u6548\u5730\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5c06Python\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u5199\u5165txt\u6587\u4ef6\uff1f<\/strong><br \/>\u8981\u5c06Python\u5217\u8868\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u9010\u884c\u5199\u5165txt\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>with open()<\/code>\u8bed\u53e5\u521b\u5efa\u6587\u4ef6\u5e76\u4f7f\u7528<code>write()<\/code>\u65b9\u6cd5\u3002\u9996\u5148\uff0c\u786e\u4fdd\u60a8\u7684\u5217\u8868\u4e2d\u5305\u542b\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u5143\u7d20\uff0c\u7136\u540e\u904d\u5386\u8be5\u5217\u8868\u5e76\u5c06\u6bcf\u4e2a\u5143\u7d20\u5199\u5165\u6587\u4ef6\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [&#39;\u5143\u7d201&#39;, &#39;\u5143\u7d202&#39;, &#39;\u5143\u7d203&#39;]\nwith open(&#39;output.txt&#39;, &#39;w&#39;, encoding=&#39;utf-8&#39;) as f:\n    for item in my_list:\n        f.write(item + &#39;\\n&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u4f1a\u5c06\u6bcf\u4e2a\u5143\u7d20\u5199\u5230\u65b0\u7684\u4e00\u884c\u3002<\/p>\n<p><strong>\u5982\u4f55\u5c06Python\u5217\u8868\u4ee5\u7279\u5b9a\u683c\u5f0f\u5199\u5165txt\u6587\u4ef6\uff1f<\/strong><br \/>\u5982\u679c\u60a8\u5e0c\u671b\u4ee5\u7279\u5b9a\u683c\u5f0f\uff08\u4f8b\u5982CSV\u683c\u5f0f\uff09\u5c06\u5217\u8868\u5199\u5165txt\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\u3002\u60a8\u53ea\u9700\u5b9a\u4e49\u5206\u9694\u7b26\u5e76\u5199\u5165\u6587\u4ef6\u3002\u4e0b\u9762\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import csv\n\nmy_list = [[&#39;\u59d3\u540d&#39;, &#39;\u5e74\u9f84&#39;], [&#39;\u5c0f\u660e&#39;, 20], [&#39;\u5c0f\u7ea2&#39;, 22]]\nwith open(&#39;output.csv&#39;, &#39;w&#39;, newline=&#39;&#39;, encoding=&#39;utf-8&#39;) as f:\n    writer = csv.writer(f)\n    writer.writerows(my_list)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u53ef\u4ee5\u751f\u6210\u4e00\u4e2a\u9017\u53f7\u5206\u9694\u7684\u6587\u4ef6\uff0c\u9002\u5408\u6570\u636e\u5206\u6790\u3002<\/p>\n<p><strong>\u5728Python\u4e2d\u5982\u4f55\u786e\u4fdd\u5199\u5165txt\u6587\u4ef6\u65f6\u4e0d\u4f1a\u4e22\u5931\u6570\u636e\uff1f<\/strong><br \/>\u786e\u4fdd\u5728\u5199\u5165\u6587\u4ef6\u4e4b\u524d\u4f7f\u7528<code>with open()<\/code>\u8bed\u53e5\uff0c\u8fd9\u6837\u53ef\u4ee5\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u81ea\u52a8\u5173\u95ed\u6587\u4ef6\u3002\u4f7f\u7528<code>&#39;a&#39;<\/code>\u6a21\u5f0f\u53ef\u4ee5\u5c06\u6570\u636e\u8ffd\u52a0\u5230\u73b0\u6709\u6587\u4ef6\u4e2d\uff0c\u800c\u4e0d\u662f\u8986\u76d6\u5b83\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">my_list = [&#39;\u65b0\u5143\u7d201&#39;, &#39;\u65b0\u5143\u7d202&#39;]\nwith open(&#39;output.txt&#39;, &#39;a&#39;, encoding=&#39;utf-8&#39;) as f:\n    for item in my_list:\n        f.write(item + &#39;\\n&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u80fd\u591f\u6709\u6548\u907f\u514d\u4e22\u5931\u73b0\u6709\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5c06\u5217\u8868\u5199\u5165\u4e00\u4e2atxt\u6587\u4ef6\uff0c\u5982\u4f7f\u7528with open\u3001writelines\u65b9\u6cd5 [&hellip;]","protected":false},"author":3,"featured_media":1117618,"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\/1117611"}],"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=1117611"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1117611\/revisions"}],"predecessor-version":[{"id":1117622,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1117611\/revisions\/1117622"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1117618"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1117611"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1117611"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1117611"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}