{"id":1015767,"date":"2024-12-27T12:10:55","date_gmt":"2024-12-27T04:10:55","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1015767.html"},"modified":"2024-12-27T12:10:58","modified_gmt":"2024-12-27T04:10:58","slug":"python%e5%a6%82%e4%bd%95%e4%bf%9d%e5%ad%98%e6%88%90txt","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1015767.html","title":{"rendered":"python\u5982\u4f55\u4fdd\u5b58\u6210txt"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25095429\/a9984642-e49f-4446-9d3a-d7e34f9d6dca.webp\" alt=\"python\u5982\u4f55\u4fdd\u5b58\u6210txt\" \/><\/p>\n<p><p> <strong>Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5c06\u6570\u636e\u4fdd\u5b58\u4e3atxt\u6587\u4ef6\uff0c\u5982\u4f7f\u7528open()\u51fd\u6570\u3001\u5229\u7528with\u8bed\u53e5\u7ba1\u7406\u6587\u4ef6\u4e0a\u4e0b\u6587\u3001\u4f7f\u7528write()\u65b9\u6cd5\u5199\u5165\u6570\u636e\u3002<\/strong> \u4f7f\u7528open()\u51fd\u6570\u53ef\u4ee5\u624b\u52a8\u7ba1\u7406\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed\u8fc7\u7a0b\uff0c\u800c\u5229\u7528with\u8bed\u53e5\u5219\u53ef\u4ee5\u66f4\u7b80\u6d01\u5730\u7ba1\u7406\u6587\u4ef6\u8d44\u6e90\uff0c\u907f\u514d\u624b\u52a8\u5173\u95ed\u6587\u4ef6\u3002\u4e0b\u9762\u5c06\u8be6\u7ec6\u4ecb\u7ecd\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\u3002<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528open()\u548cwrite()\u65b9\u6cd5\u4fdd\u5b58\u6570\u636e<\/strong><\/p>\n<\/p>\n<p><p>\u8981\u5c06\u6570\u636e\u4fdd\u5b58\u5230txt\u6587\u4ef6\u4e2d\uff0c\u9996\u5148\u9700\u8981\u4f7f\u7528open()\u51fd\u6570\u6253\u5f00\u6587\u4ef6\u3002open()\u51fd\u6570\u63a5\u53d7\u6587\u4ef6\u540d\u548c\u6a21\u5f0f\u4f5c\u4e3a\u53c2\u6570\uff0c\u5176\u4e2d\u6a21\u5f0f\u5305\u62ec\u8bfb\u53d6\u6a21\u5f0f(&#39;r&#39;)\u3001\u5199\u5165\u6a21\u5f0f(&#39;w&#39;)\u3001\u8ffd\u52a0\u6a21\u5f0f(&#39;a&#39;)\u7b49\u3002\u5f53\u6587\u4ef6\u4e0d\u5b58\u5728\u65f6\uff0c\u5199\u5165\u6a21\u5f0f\u548c\u8ffd\u52a0\u6a21\u5f0f\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\u3002\u7136\u540e\uff0c\u4f7f\u7528write()\u65b9\u6cd5\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\uff0c\u6700\u540e\u8c03\u7528close()\u65b9\u6cd5\u5173\u95ed\u6587\u4ef6\u4ee5\u91ca\u653e\u8d44\u6e90\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u6587\u4ef6\uff0c\u6a21\u5f0f\u4e3a\u5199\u5165\u6a21\u5f0f<\/p>\n<p>file = open(&quot;example.txt&quot;, &quot;w&quot;)<\/p>\n<h2><strong>\u5199\u5165\u6570\u636e\u5230\u6587\u4ef6<\/strong><\/h2>\n<p>file.write(&quot;\u8fd9\u662f\u7b2c\u4e00\u884c\u6587\u672c\u3002\\n&quot;)<\/p>\n<p>file.write(&quot;\u8fd9\u662f\u7b2c\u4e8c\u884c\u6587\u672c\u3002\\n&quot;)<\/p>\n<h2><strong>\u5173\u95ed\u6587\u4ef6<\/strong><\/h2>\n<p>file.close()<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u63a5\u4e0b\u6765\uff0c\u6211\u4eec\u5c06\u5bf9Python\u4fdd\u5b58txt\u6587\u4ef6\u7684\u5176\u4ed6\u65b9\u6cd5\u548c\u7ec6\u8282\u8fdb\u884c\u66f4\u6df1\u5165\u7684\u63a2\u8ba8\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528WITH\u8bed\u53e5\u4fdd\u5b58\u6570\u636e<\/strong><\/h2>\n<p><p>\u4f7f\u7528with\u8bed\u53e5\u53ef\u4ee5\u81ea\u52a8\u7ba1\u7406\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed\uff0c\u65e0\u9700\u663e\u5f0f\u8c03\u7528close()\u65b9\u6cd5\u3002\u8fd9\u79cd\u65b9\u5f0f\u4e0d\u4ec5\u7b80\u5316\u4e86\u4ee3\u7801\uff0c\u8fd8\u80fd\u6709\u6548\u907f\u514d\u56e0\u5fd8\u8bb0\u5173\u95ed\u6587\u4ef6\u800c\u5bfc\u81f4\u7684\u8d44\u6e90\u6cc4\u6f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;example.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(&quot;\u8fd9\u662f\u7b2c\u4e00\u884c\u6587\u672c\u3002\\n&quot;)<\/p>\n<p>    file.write(&quot;\u8fd9\u662f\u7b2c\u4e8c\u884c\u6587\u672c\u3002\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u4e0a\u9762\u7684\u4ee3\u7801\u4e2d\uff0c\u6587\u4ef6\u5728with\u8bed\u53e5\u7684\u4e0a\u4e0b\u6587\u7ed3\u675f\u65f6\u81ea\u52a8\u5173\u95ed\u3002\u4f7f\u7528with\u8bed\u53e5\u6709\u52a9\u4e8e\u63d0\u9ad8\u4ee3\u7801\u7684\u53ef\u8bfb\u6027\u548c\u5b89\u5168\u6027\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u591a\u4e2a\u6587\u4ef6\u6216\u8fdb\u884c\u590d\u6742\u7684\u6587\u4ef6\u64cd\u4f5c\u65f6\u3002<\/p>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528APPEND\u6a21\u5f0f\u8ffd\u52a0\u6570\u636e<\/strong><\/h2>\n<p><p>\u6709\u65f6\u6211\u4eec\u9700\u8981\u5728\u73b0\u6709\u7684txt\u6587\u4ef6\u540e\u8ffd\u52a0\u6570\u636e\uff0c\u800c\u4e0d\u662f\u8986\u76d6\u539f\u6709\u5185\u5bb9\u3002\u8fd9\u65f6\u53ef\u4ee5\u4f7f\u7528\u8ffd\u52a0\u6a21\u5f0f(&#39;a&#39;)\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;example.txt&quot;, &quot;a&quot;) as file:<\/p>\n<p>    file.write(&quot;\u8fd9\u662f\u8ffd\u52a0\u7684\u4e00\u884c\u6587\u672c\u3002\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u8ffd\u52a0\u6a21\u5f0f\u4f1a\u5728\u6587\u4ef6\u672b\u5c3e\u6dfb\u52a0\u65b0\u6570\u636e\uff0c\u800c\u4e0d\u4f1a\u5220\u9664\u539f\u6709\u5185\u5bb9\u3002\u9700\u8981\u6ce8\u610f\u7684\u662f\uff0c\u5982\u679c\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u8ffd\u52a0\u6a21\u5f0f\u4f1a\u521b\u5efa\u4e00\u4e2a\u65b0\u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u5904\u7406\u4e0d\u540c\u7684\u6570\u636e\u7c7b\u578b<\/strong><\/h2>\n<p><p>\u5728\u5199\u5165txt\u6587\u4ef6\u65f6\uff0c\u901a\u5e38\u9700\u8981\u5c06\u6570\u636e\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u683c\u5f0f\u3002Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u5c06\u5176\u4ed6\u6570\u636e\u7c7b\u578b\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982\u4f7f\u7528str()\u51fd\u6570\u3001\u683c\u5f0f\u5316\u5b57\u7b26\u4e32\u7b49\u3002<\/p>\n<\/p>\n<p><h2>1. \u5199\u5165\u6574\u6570\u548c\u6d6e\u70b9\u6570<\/h2>\n<\/p>\n<p><p>\u5f53\u6570\u636e\u4e3a\u6574\u6570\u6216\u6d6e\u70b9\u6570\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528str()\u51fd\u6570\u8fdb\u884c\u8f6c\u6362\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">number = 42<\/p>\n<p>pi = 3.14159<\/p>\n<p>with open(&quot;example.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(&quot;\u6574\u6570: &quot; + str(number) + &quot;\\n&quot;)<\/p>\n<p>    file.write(&quot;\u6d6e\u70b9\u6570: &quot; + str(pi) + &quot;\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h2>2. \u5199\u5165\u5217\u8868\u548c\u5b57\u5178<\/h2>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5217\u8868\u548c\u5b57\u5178\uff0c\u53ef\u4ee5\u4f7f\u7528json\u6a21\u5757\u5c06\u5176\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u7684\u5b57\u7b26\u4e32\uff0c\u65b9\u4fbf\u5b58\u50a8\u548c\u8bfb\u53d6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data_list = [1, 2, 3, 4, 5]<\/p>\n<p>data_dict = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30}<\/p>\n<p>with open(&quot;example.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(&quot;\u5217\u8868: &quot; + json.dumps(data_list) + &quot;\\n&quot;)<\/p>\n<p>    file.write(&quot;\u5b57\u5178: &quot; + json.dumps(data_dict) + &quot;\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u56db\u3001\u9010\u884c\u5199\u5165\u6570\u636e<\/strong><\/h2>\n<p><p>\u5728\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\u65f6\uff0c\u9010\u884c\u5199\u5165\u6570\u636e\u53ef\u4ee5\u6709\u6548\u8282\u7701\u5185\u5b58\u3002\u53ef\u4ee5\u4f7f\u7528\u5faa\u73af\u904d\u5386\u6570\u636e\u96c6\uff0c\u5e76\u5728\u6bcf\u6b21\u8fed\u4ee3\u4e2d\u5c06\u4e00\u884c\u6570\u636e\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = [&quot;\u884c1&quot;, &quot;\u884c2&quot;, &quot;\u884c3&quot;, &quot;\u884c4&quot;]<\/p>\n<p>with open(&quot;example.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    for line in data:<\/p>\n<p>        file.write(line + &quot;\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u9010\u884c\u5199\u5165\u6709\u52a9\u4e8e\u5904\u7406\u5927\u578b\u6570\u636e\u96c6\uff0c\u5982\u65e5\u5fd7\u6587\u4ef6\u6216\u6570\u636e\u5206\u6790\u7ed3\u679c\uff0c\u907f\u514d\u4e00\u6b21\u6027\u5c06\u6240\u6709\u6570\u636e\u52a0\u8f7d\u5230\u5185\u5b58\u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u4e94\u3001\u8bfb\u53d6\u5e76\u4fee\u6539\u6587\u4ef6\u5185\u5bb9<\/strong><\/h2>\n<p><p>\u6709\u65f6\u9700\u8981\u8bfb\u53d6txt\u6587\u4ef6\u7684\u5185\u5bb9\uff0c\u8fdb\u884c\u4fee\u6539\u540e\u518d\u4fdd\u5b58\u3002\u53ef\u4ee5\u5148\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5230\u5185\u5b58\u4e2d\uff0c\u8fdb\u884c\u4fee\u6539\u540e\u518d\u5199\u5165\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\"># \u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9<\/p>\n<p>with open(&quot;example.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>    lines = file.readlines()<\/p>\n<h2><strong>\u4fee\u6539\u5185\u5bb9<\/strong><\/h2>\n<p>lines[0] = &quot;\u4fee\u6539\u540e\u7684\u7b2c\u4e00\u884c\\n&quot;<\/p>\n<h2><strong>\u5199\u56de\u6587\u4ef6<\/strong><\/h2>\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><p>\u5728\u4e0a\u8ff0\u4ee3\u7801\u4e2d\uff0c\u9996\u5148\u4f7f\u7528readlines()\u65b9\u6cd5\u8bfb\u53d6\u6587\u4ef6\u6240\u6709\u884c\uff0c\u5e76\u5c06\u5176\u5b58\u50a8\u4e3a\u5217\u8868\u3002\u7136\u540e\u5bf9\u5217\u8868\u8fdb\u884c\u4fee\u6539\uff0c\u6700\u540e\u4f7f\u7528writelines()\u65b9\u6cd5\u5c06\u4fee\u6539\u540e\u7684\u5185\u5bb9\u5199\u56de\u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u516d\u3001\u4f7f\u7528CSV\u6a21\u5757\u5904\u7406\u6587\u672c\u6587\u4ef6<\/strong><\/h2>\n<p><p>\u5f53\u9700\u8981\u5904\u7406\u4ee5\u9017\u53f7\u5206\u9694\u7684\u6587\u672c\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528Python\u7684csv\u6a21\u5757\u3002csv\u6a21\u5757\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u65b9\u6cd5\u5904\u7406CSV\u683c\u5f0f\u7684\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><h2>1. \u5199\u5165CSV\u6587\u4ef6<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>data = [[&quot;\u59d3\u540d&quot;, &quot;\u5e74\u9f84&quot;], [&quot;Alice&quot;, 30], [&quot;Bob&quot;, 25]]<\/p>\n<p>with open(&quot;example.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><h2>2. \u8bfb\u53d6CSV\u6587\u4ef6<\/h2>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>with open(&quot;example.csv&quot;, &quot;r&quot;) as file:<\/p>\n<p>    reader = csv.reader(file)<\/p>\n<p>    for row in reader:<\/p>\n<p>        print(row)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4f7f\u7528csv\u6a21\u5757\u53ef\u4ee5\u8f7b\u677e\u5904\u7406CSV\u683c\u5f0f\u7684\u6587\u4ef6\uff0c\u5305\u62ec\u8bfb\u53d6\u3001\u5199\u5165\u548c\u89e3\u6790\u3002<\/p>\n<\/p>\n<h2><strong>\u4e03\u3001\u5904\u7406\u7f16\u7801\u95ee\u9898<\/strong><\/h2>\n<p><p>\u5728\u5904\u7406\u975eASCII\u5b57\u7b26\u65f6\uff0c\u53ef\u80fd\u9700\u8981\u6307\u5b9a\u6587\u4ef6\u7f16\u7801\u3002\u4f8b\u5982\uff0c\u5bf9\u4e8e\u5305\u542b\u4e2d\u6587\u5b57\u7b26\u7684\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528UTF-8\u7f16\u7801\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;example.txt&quot;, &quot;w&quot;, encoding=&quot;utf-8&quot;) as file:<\/p>\n<p>    file.write(&quot;\u8fd9\u662f\u5305\u542b\u4e2d\u6587\u7684\u6587\u672c\u3002\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u6307\u5b9a\u7f16\u7801\u6709\u52a9\u4e8e\u907f\u514d\u56e0\u5b57\u7b26\u7f16\u7801\u4e0d\u5339\u914d\u800c\u5bfc\u81f4\u7684\u4e71\u7801\u95ee\u9898\uff0c\u5c24\u5176\u662f\u5728\u5904\u7406\u591a\u8bed\u8a00\u6587\u672c\u65f6\u3002<\/p>\n<\/p>\n<h2><strong>\u516b\u3001\u603b\u7ed3<\/strong><\/h2>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u5f0f\u5c06\u6570\u636e\u4fdd\u5b58\u4e3atxt\u6587\u4ef6\uff0c\u5305\u62ec\u4f7f\u7528open()\u51fd\u6570\u3001with\u8bed\u53e5\u3001write()\u65b9\u6cd5\u7b49\u3002\u4e0d\u540c\u7684\u65b9\u6cd5\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\uff0c\u5982\u9010\u884c\u5199\u5165\u3001\u8ffd\u52a0\u5199\u5165\u3001\u5904\u7406CSV\u6587\u4ef6\u7b49\u3002\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u9700\u8981\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u5e76\u6ce8\u610f\u5904\u7406\u7f16\u7801\u95ee\u9898\u4ee5\u907f\u514d\u4e71\u7801\u3002\u901a\u8fc7\u5408\u7406\u4f7f\u7528\u8fd9\u4e9b\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u7ba1\u7406\u548c\u5b58\u50a8\u6587\u672c\u6570\u636e\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u6570\u636e\u4fdd\u5b58\u4e3atxt\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u6216\u6253\u5f00\u4e00\u4e2a\u6587\u672c\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>write()<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u3002\u4f8b\u5982\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u4ee3\u7801\u5c06\u5b57\u7b26\u4e32\u4fdd\u5b58\u4e3atxt\u6587\u4ef6\uff1a  <\/p>\n<pre><code class=\"language-python\">data = &quot;\u8fd9\u662f\u8981\u4fdd\u5b58\u5230txt\u6587\u4ef6\u7684\u5185\u5bb9\u3002&quot;\nwith open(&quot;output.txt&quot;, &quot;w&quot;, encoding=&quot;utf-8&quot;) as file:\n    file.write(data)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u6cd5\u4f1a\u5728\u5f53\u524d\u5de5\u4f5c\u76ee\u5f55\u4e0b\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>output.txt<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u5c06\u6307\u5b9a\u5185\u5bb9\u5199\u5165\u5176\u4e2d\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u5c06\u54ea\u4e9b\u7c7b\u578b\u7684\u6570\u636e\u4fdd\u5b58\u4e3atxt\u6587\u4ef6\uff1f<\/strong><br \/>\u51e0\u4e4e\u6240\u6709\u7c7b\u578b\u7684\u6570\u636e\u90fd\u53ef\u4ee5\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\u5e76\u4fdd\u5b58\u4e3atxt\u6587\u4ef6\uff0c\u5305\u62ec\u6587\u672c\u3001\u6570\u5b57\u3001\u5217\u8868\u3001\u5b57\u5178\u7b49\u3002\u5bf9\u4e8e\u590d\u6742\u7684\u6570\u636e\u7c7b\u578b\uff0c\u4f8b\u5982\u5217\u8868\u6216\u5b57\u5178\uff0c\u53ef\u4ee5\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u5c06\u5176\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\uff0c\u7136\u540e\u5199\u5165txt\u6587\u4ef6\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">import json\n\ndata = {&quot;name&quot;: &quot;Alice&quot;, &quot;age&quot;: 30}\nwith open(&quot;data.txt&quot;, &quot;w&quot;, encoding=&quot;utf-8&quot;) as file:\n    json.dump(data, file)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u6570\u636e\u7684\u7ed3\u6784\u5728\u8bfb\u53d6\u65f6\u80fd\u591f\u88ab\u6b63\u786e\u89e3\u6790\u3002<\/p>\n<p><strong>\u5982\u4f55\u4ecetxt\u6587\u4ef6\u4e2d\u8bfb\u53d6\u6570\u636e\uff1f<\/strong><br \/>\u8bfb\u53d6txt\u6587\u4ef6\u540c\u6837\u7b80\u5355\u3002\u4f7f\u7528<code>open()<\/code>\u51fd\u6570\u548c<code>read()<\/code>\u6216<code>readlines()<\/code>\u65b9\u6cd5\u53ef\u4ee5\u8f7b\u677e\u5b9e\u73b0\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&quot;output.txt&quot;, &quot;r&quot;, encoding=&quot;utf-8&quot;) as file:\n    content = file.read()\nprint(content)\n<\/code><\/pre>\n<p>\u8fd9\u6bb5\u4ee3\u7801\u5c06\u6253\u5f00<code>output.txt<\/code>\u6587\u4ef6\u5e76\u6253\u5370\u51fa\u5176\u4e2d\u7684\u5185\u5bb9\uff0c\u786e\u4fdd\u60a8\u80fd\u591f\u8bbf\u95ee\u4e4b\u524d\u4fdd\u5b58\u7684\u6570\u636e\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5c06\u6570\u636e\u4fdd\u5b58\u4e3atxt\u6587\u4ef6\uff0c\u5982\u4f7f\u7528open()\u51fd\u6570\u3001\u5229\u7528with\u8bed\u53e5\u7ba1\u7406\u6587\u4ef6\u4e0a\u4e0b\u6587\u3001\u4f7f [&hellip;]","protected":false},"author":3,"featured_media":1015778,"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\/1015767"}],"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=1015767"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1015767\/revisions"}],"predecessor-version":[{"id":1015780,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1015767\/revisions\/1015780"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1015778"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1015767"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1015767"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1015767"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}