{"id":1187550,"date":"2025-01-15T20:06:52","date_gmt":"2025-01-15T12:06:52","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1187550.html"},"modified":"2025-01-15T20:06:55","modified_gmt":"2025-01-15T12:06:55","slug":"python-%e5%a6%82%e4%bd%95%e7%bb%93%e6%9e%9c%e8%be%93%e5%87%ba%e5%88%b0%e6%96%87%e4%bb%b6","status":"publish","type":"post","link":"https:\/\/docs.pingcode.com\/ask\/1187550.html","title":{"rendered":"python \u5982\u4f55\u7ed3\u679c\u8f93\u51fa\u5230\u6587\u4ef6"},"content":{"rendered":"<p style=\"text-align:center;\" ><img decoding=\"async\" src=\"https:\/\/cdn-kb.worktile.com\/kb\/wp-content\/uploads\/2024\/04\/25140116\/a8b36ddd-3f81-4e84-8072-d90c472a4fc9.webp\" alt=\"python \u5982\u4f55\u7ed3\u679c\u8f93\u51fa\u5230\u6587\u4ef6\" \/><\/p>\n<p><p> <strong>Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5c06\u7ed3\u679c\u8f93\u51fa\u5230\u6587\u4ef6\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684<code>open<\/code>\u51fd\u6570\u6765\u6253\u5f00\u6587\u4ef6\u3001\u4f7f\u7528<code>write<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u3001\u4f7f\u7528<code>print<\/code>\u51fd\u6570\u7684<code>file<\/code>\u53c2\u6570\u6307\u5b9a\u8f93\u51fa\u6587\u4ef6\uff0c\u4ee5\u53ca\u4f7f\u7528<code>csv<\/code>\u6216<code>json<\/code>\u6a21\u5757\u6765\u5904\u7406\u7279\u5b9a\u683c\u5f0f\u7684\u6570\u636e\u3002<\/strong>\u5176\u4e2d\uff0c\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u7ed3\u5408<code>write<\/code>\u65b9\u6cd5\u662f\u6700\u5e38\u89c1\u548c\u57fa\u7840\u7684\u65b9\u6cd5\u3002\u901a\u8fc7\u8fd9\u79cd\u65b9\u5f0f\uff0c\u53ef\u4ee5\u7cbe\u786e\u63a7\u5236\u6587\u4ef6\u7684\u8bfb\u5199\u6a21\u5f0f\u3001\u7f16\u7801\u683c\u5f0f\u7b49\u3002<\/p>\n<\/p>\n<p><h3>\u4e00\u3001\u4f7f\u7528<code>open<\/code>\u51fd\u6570\u548c<code>write<\/code>\u65b9\u6cd5<\/h3>\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\u6307\u5b9a\u6587\u4ef6\u540d\u548c\u6a21\u5f0f\uff08\u5982\u8bfb\u3001\u5199\u3001\u8ffd\u52a0\u7b49\uff09\u3002<\/li>\n<li><strong>\u5199\u5165\u6570\u636e<\/strong>\uff1a\u4f7f\u7528\u6587\u4ef6\u5bf9\u8c61\u7684<code>write<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\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\u786e\u4fdd\u6570\u636e\u5b8c\u6574\u5199\u5165\u3002<\/li>\n<\/ol>\n<p><pre><code class=\"language-python\"># \u6253\u5f00\u6587\u4ef6\uff0c\u6a21\u5f0f\u4e3a\u5199\uff08&#39;w&#39;\uff09<\/p>\n<p>file = open(&quot;output.txt&quot;, &quot;w&quot;)<\/p>\n<h2><strong>\u5199\u5165\u6570\u636e<\/strong><\/h2>\n<p>file.write(&quot;\u8fd9\u662f\u7b2c\u4e00\u884c\u6570\u636e\\n&quot;)<\/p>\n<p>file.write(&quot;\u8fd9\u662f\u7b2c\u4e8c\u884c\u6570\u636e\\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><h3>\u4e8c\u3001\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u7ba1\u7406\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u53ef\u4ee5\u66f4\u7b80\u6d01\u5730\u7ba1\u7406\u6587\u4ef6\u7684\u6253\u5f00\u548c\u5173\u95ed\uff0c\u907f\u514d\u56e0\u5f02\u5e38\u5bfc\u81f4\u7684\u6587\u4ef6\u672a\u5173\u95ed\u95ee\u9898\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;output.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(&quot;\u8fd9\u662f\u7b2c\u4e00\u884c\u6570\u636e\\n&quot;)<\/p>\n<p>    file.write(&quot;\u8fd9\u662f\u7b2c\u4e8c\u884c\u6570\u636e\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e09\u3001\u4f7f\u7528<code>print<\/code>\u51fd\u6570\u8f93\u51fa\u5230\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u4f7f\u7528<code>print<\/code>\u51fd\u6570\u65f6\uff0c\u53ef\u4ee5\u901a\u8fc7<code>file<\/code>\u53c2\u6570\u6307\u5b9a\u8f93\u51fa\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;output.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    print(&quot;\u8fd9\u662f\u7b2c\u4e00\u884c\u6570\u636e&quot;, file=file)<\/p>\n<p>    print(&quot;\u8fd9\u662f\u7b2c\u4e8c\u884c\u6570\u636e&quot;, file=file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u56db\u3001\u5904\u7406\u7279\u5b9a\u683c\u5f0f\u7684\u6570\u636e<\/h3>\n<\/p>\n<p><h4>1. CSV\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u8868\u683c\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>csv<\/code>\u6a21\u5757\u65b9\u4fbf\u5730\u5199\u5165CSV\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<p>data = [<\/p>\n<p>    [&quot;\u59d3\u540d&quot;, &quot;\u5e74\u9f84&quot;, &quot;\u57ce\u5e02&quot;],<\/p>\n<p>    [&quot;\u5f20\u4e09&quot;, 25, &quot;\u5317\u4eac&quot;],<\/p>\n<p>    [&quot;\u674e\u56db&quot;, 30, &quot;\u4e0a\u6d77&quot;]<\/p>\n<p>]<\/p>\n<p>with open(&quot;output.csv&quot;, &quot;w&quot;, newline=&quot;&quot;) 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>2. JSON\u6587\u4ef6<\/h4>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5d4c\u5957\u6570\u636e\u7ed3\u6784\uff0c\u53ef\u4ee5\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u5199\u5165JSON\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {<\/p>\n<p>    &quot;name&quot;: &quot;\u5f20\u4e09&quot;,<\/p>\n<p>    &quot;age&quot;: 25,<\/p>\n<p>    &quot;city&quot;: &quot;\u5317\u4eac&quot;<\/p>\n<p>}<\/p>\n<p>with open(&quot;output.json&quot;, &quot;w&quot;) as file:<\/p>\n<p>    json.dump(data, file, ensure_ascii=False, indent=4)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e94\u3001\u4e8c\u8fdb\u5236\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u53ef\u4ee5\u4f7f\u7528<code>wb<\/code>\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>write<\/code>\u65b9\u6cd5\u5199\u5165\u5b57\u8282\u6570\u636e\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">data = b&#39;\\x00\\x01\\x02\\x03\\x04\\x05&#39;<\/p>\n<p>with open(&quot;output.bin&quot;, &quot;wb&quot;) as file:<\/p>\n<p>    file.write(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516d\u3001\u8ffd\u52a0\u6a21\u5f0f<\/h3>\n<\/p>\n<p><p>\u5f53\u9700\u8981\u5728\u6587\u4ef6\u672b\u5c3e\u8ffd\u52a0\u6570\u636e\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>a<\/code>\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;output.txt&quot;, &quot;a&quot;) as file:<\/p>\n<p>    file.write(&quot;\u8fd9\u662f\u8ffd\u52a0\u7684\u6570\u636e\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e03\u3001\u6587\u4ef6\u8bfb\u53d6\u548c\u5199\u5165\u7684\u7ed3\u5408<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u8bfb\u53d6\u6587\u4ef6\u5185\u5bb9\u5e76\u8fdb\u884c\u5904\u7406\u540e\u518d\u5199\u56de\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;input.txt&quot;, &quot;r&quot;) as file:<\/p>\n<p>    data = file.read()<\/p>\n<h2><strong>\u5904\u7406\u6570\u636e<\/strong><\/h2>\n<p>processed_data = data.upper()<\/p>\n<h2><strong>\u5199\u56de\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&quot;output.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>    file.write(processed_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u516b\u3001\u5927\u6587\u4ef6\u7684\u9010\u884c\u8bfb\u53d6\u548c\u5199\u5165<\/h3>\n<\/p>\n<p><p>\u5bf9\u4e8e\u5927\u6587\u4ef6\uff0c\u53ef\u4ee5\u9010\u884c\u8bfb\u53d6\u548c\u5199\u5165\uff0c\u907f\u514d\u4e00\u6b21\u6027\u8bfb\u53d6\u5bfc\u81f4\u5185\u5b58\u5360\u7528\u8fc7\u9ad8\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;input.txt&quot;, &quot;r&quot;) as infile, open(&quot;output.txt&quot;, &quot;w&quot;) as outfile:<\/p>\n<p>    for line in infile:<\/p>\n<p>        processed_line = line.upper()  # \u793a\u4f8b\u5904\u7406\uff1a\u8f6c\u6362\u4e3a\u5927\u5199<\/p>\n<p>        outfile.write(processed_line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u4e5d\u3001\u5f02\u5e38\u5904\u7406<\/h3>\n<\/p>\n<p><p>\u5728\u6587\u4ef6\u64cd\u4f5c\u4e2d\uff0c\u5e94\u8003\u8651\u5230\u53ef\u80fd\u7684\u5f02\u5e38\u60c5\u51b5\uff0c\u4f7f\u7528<code>try-except<\/code>\u8bed\u53e5\u8fdb\u884c\u5f02\u5e38\u5904\u7406\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    with open(&quot;output.txt&quot;, &quot;w&quot;) as file:<\/p>\n<p>        file.write(&quot;\u8fd9\u662f\u4e00\u4e9b\u6570\u636e\\n&quot;)<\/p>\n<p>except IOError as e:<\/p>\n<p>    print(f&quot;\u6587\u4ef6\u64cd\u4f5c\u5931\u8d25: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u3001\u66f4\u591a\u6587\u4ef6\u64cd\u4f5c\u6a21\u5f0f<\/h3>\n<\/p>\n<p><p>Python\u7684<code>open<\/code>\u51fd\u6570\u652f\u6301\u591a\u79cd\u6587\u4ef6\u64cd\u4f5c\u6a21\u5f0f\uff0c\u5982\u8bfb\u5199\u3001\u4e8c\u8fdb\u5236\u6a21\u5f0f\u7b49\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>&#39;r&#39;<\/code>\uff1a\u53ea\u8bfb\uff08\u9ed8\u8ba4\u6a21\u5f0f\uff09<\/li>\n<li><code>&#39;w&#39;<\/code>\uff1a\u53ea\u5199\uff08\u4f1a\u8986\u76d6\u6587\u4ef6\u5185\u5bb9\uff09<\/li>\n<li><code>&#39;a&#39;<\/code>\uff1a\u8ffd\u52a0<\/li>\n<li><code>&#39;b&#39;<\/code>\uff1a\u4e8c\u8fdb\u5236\u6a21\u5f0f<\/li>\n<li><code>&#39;+&#39;<\/code>\uff1a\u8bfb\u5199\u6a21\u5f0f<\/li>\n<\/ul>\n<p><pre><code class=\"language-python\"># \u8bfb\u5199\u6a21\u5f0f<\/p>\n<p>with open(&quot;output.txt&quot;, &quot;r+&quot;) as file:<\/p>\n<p>    content = file.read()<\/p>\n<p>    file.write(&quot;\\n\u8fd9\u662f\u8ffd\u52a0\u7684\u6570\u636e&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e00\u3001\u591a\u79cd\u7f16\u7801\u683c\u5f0f<\/h3>\n<\/p>\n<p><p>\u5728\u5904\u7406\u4e0d\u540c\u7f16\u7801\u683c\u5f0f\u7684\u6587\u4ef6\u65f6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>encoding<\/code>\u53c2\u6570\u6307\u5b9a\u7f16\u7801\u683c\u5f0f\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&quot;output.txt&quot;, &quot;w&quot;, encoding=&quot;utf-8&quot;) as file:<\/p>\n<p>    file.write(&quot;\u8fd9\u662f\u4e00\u4e9b\u4f7f\u7528UTF-8\u7f16\u7801\u7684\u6570\u636e\\n&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e8c\u3001\u4e34\u65f6\u6587\u4ef6<\/h3>\n<\/p>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u53ef\u80fd\u9700\u8981\u4f7f\u7528\u4e34\u65f6\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528<code>tempfile<\/code>\u6a21\u5757\u521b\u5efa\u4e34\u65f6\u6587\u4ef6\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import tempfile<\/p>\n<p>with tempfile.NamedTemporaryFile(delete=False) as temp_file:<\/p>\n<p>    temp_file.write(b&quot;\u8fd9\u662f\u4e00\u4e9b\u4e34\u65f6\u6570\u636e\\n&quot;)<\/p>\n<p>    temp_file_path = temp_file.name<\/p>\n<p>print(f&quot;\u4e34\u65f6\u6587\u4ef6\u8def\u5f84: {temp_file_path}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><h3>\u5341\u4e09\u3001\u603b\u7ed3<\/h3>\n<\/p>\n<p><p>Python\u63d0\u4f9b\u4e86\u591a\u79cd\u65b9\u6cd5\u5c06\u7ed3\u679c\u8f93\u51fa\u5230\u6587\u4ef6\uff0c\u9002\u7528\u4e8e\u4e0d\u540c\u7684\u5e94\u7528\u573a\u666f\u3002\u901a\u8fc7\u9009\u62e9\u5408\u9002\u7684\u65b9\u6cd5\uff0c\u53ef\u4ee5\u9ad8\u6548\u5730\u5904\u7406\u6587\u4ef6\u64cd\u4f5c\uff0c\u4fdd\u8bc1\u6570\u636e\u7684\u6b63\u786e\u6027\u548c\u5b8c\u6574\u6027\u3002\u5728\u5b9e\u9645\u5f00\u53d1\u4e2d\uff0c\u5e94\u6839\u636e\u5177\u4f53\u9700\u6c42\u9009\u62e9\u5408\u9002\u7684\u6587\u4ef6\u64cd\u4f5c\u65b9\u5f0f\uff0c\u5e76\u6ce8\u610f\u6587\u4ef6\u7684\u5173\u95ed\u3001\u5f02\u5e38\u5904\u7406\u7b49\u7ec6\u8282\uff0c\u4ee5\u786e\u4fdd\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u8f93\u51fa\u7ed3\u679c\u4fdd\u5b58\u5230\u6587\u672c\u6587\u4ef6\uff1f<\/strong><br \/>\u5728Python\u4e2d\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684<code>open()<\/code>\u51fd\u6570\u6765\u521b\u5efa\u6216\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\uff0c\u5e76\u4f7f\u7528<code>write()<\/code>\u65b9\u6cd5\u5c06\u7ed3\u679c\u5199\u5165\u6587\u4ef6\u3002\u9996\u5148\uff0c\u786e\u4fdd\u4ee5\u5199\u6a21\u5f0f\uff08&#39;w&#39;\uff09\u6216\u8ffd\u52a0\u6a21\u5f0f\uff08&#39;a&#39;\uff09\u6253\u5f00\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">result = &quot;\u8fd9\u662f\u8981\u8f93\u51fa\u7684\u7ed3\u679c\u3002&quot;\nwith open(&#39;output.txt&#39;, &#39;w&#39;) as file:\n    file.write(result)\n<\/code><\/pre>\n<p>\u4f7f\u7528<code>with<\/code>\u8bed\u53e5\u80fd\u591f\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u6b63\u786e\u5173\u95ed\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u5c06\u54ea\u4e9b\u7c7b\u578b\u7684\u6570\u636e\u8f93\u51fa\u5230\u6587\u4ef6\u4e2d\uff1f<\/strong><br \/>Python\u5141\u8bb8\u60a8\u5c06\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5217\u8868\u3001\u5b57\u5178\u7b49\u6570\u636e\u7c7b\u578b\u8f93\u51fa\u5230\u6587\u4ef6\u4e2d\u3002\u5bf9\u4e8e\u975e\u5b57\u7b26\u4e32\u7c7b\u578b\u7684\u6570\u636e\uff0c\u60a8\u9700\u8981\u5148\u5c06\u5176\u8f6c\u6362\u4e3a\u5b57\u7b26\u4e32\uff0c\u4f8b\u5982\u4f7f\u7528<code>str()<\/code>\u51fd\u6570\u3002\u5bf9\u4e8e\u66f4\u590d\u6742\u7684\u6570\u636e\u7ed3\u6784\uff0c\u60a8\u53ef\u4ee5\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u5c06\u5176\u5e8f\u5217\u5316\u4e3aJSON\u683c\u5f0f\uff0c\u7136\u540e\u5199\u5165\u6587\u4ef6\u3002\u793a\u4f8b\u5982\u4e0b\uff1a  <\/p>\n<pre><code class=\"language-python\">import json\ndata = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30}\nwith open(&#39;data.json&#39;, &#39;w&#39;) as file:\n    json.dump(data, file)\n<\/code><\/pre>\n<p><strong>\u5982\u4f55\u5728Python\u4e2d\u5904\u7406\u6587\u4ef6\u8f93\u51fa\u7684\u9519\u8bef\uff1f<\/strong><br \/>\u5728\u6587\u4ef6\u64cd\u4f5c\u8fc7\u7a0b\u4e2d\uff0c\u53ef\u80fd\u4f1a\u51fa\u73b0\u5404\u79cd\u9519\u8bef\uff0c\u4f8b\u5982\u6587\u4ef6\u6743\u9650\u95ee\u9898\u6216\u78c1\u76d8\u7a7a\u95f4\u4e0d\u8db3\u3002\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u53ef\u4ee5\u6709\u6548\u6355\u83b7\u5e76\u5904\u7406\u8fd9\u4e9b\u5f02\u5e38\u3002\u4f8b\u5982\uff1a  <\/p>\n<pre><code class=\"language-python\">try:\n    with open(&#39;output.txt&#39;, &#39;w&#39;) as file:\n        file.write(&quot;\u5199\u5165\u6587\u4ef6\u5185\u5bb9&quot;)\nexcept IOError as e:\n    print(f&quot;\u53d1\u751f\u9519\u8bef\uff1a{e}&quot;)\n<\/code><\/pre>\n<p>\u8fd9\u79cd\u65b9\u5f0f\u80fd\u591f\u63d0\u9ad8\u7a0b\u5e8f\u7684\u5065\u58ee\u6027\uff0c\u786e\u4fdd\u5373\u4f7f\u5728\u51fa\u9519\u7684\u60c5\u51b5\u4e0b\u4e5f\u80fd\u63d0\u4f9b\u6709\u7528\u7684\u4fe1\u606f\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"Python\u4e2d\u53ef\u4ee5\u901a\u8fc7\u591a\u79cd\u65b9\u5f0f\u5c06\u7ed3\u679c\u8f93\u51fa\u5230\u6587\u4ef6\u3002\u5e38\u89c1\u7684\u65b9\u6cd5\u5305\u62ec\u4f7f\u7528\u5185\u7f6e\u7684open\u51fd\u6570\u6765\u6253\u5f00\u6587\u4ef6\u3001\u4f7f\u7528write [&hellip;]","protected":false},"author":3,"featured_media":1187560,"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\/1187550"}],"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=1187550"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1187550\/revisions"}],"predecessor-version":[{"id":1187564,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1187550\/revisions\/1187564"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1187560"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1187550"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1187550"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1187550"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}