{"id":1182819,"date":"2025-01-15T19:07:39","date_gmt":"2025-01-15T11:07:39","guid":{"rendered":"https:\/\/docs.pingcode.com\/ask\/ask-ask\/1182819.html"},"modified":"2025-01-15T19:07:41","modified_gmt":"2025-01-15T11:07:41","slug":"python%e5%a6%82%e4%bd%95%e6%8a%8a%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\/1182819.html","title":{"rendered":"python\u5982\u4f55\u628a\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\/25130711\/9b1daa1a-b37a-48ed-84e7-d26327d7db24.webp\" alt=\"python\u5982\u4f55\u628a\u8f93\u51fa\u5230\u6587\u4ef6\" \/><\/p>\n<p><p> \u8981\u5c06 Python \u8f93\u51fa\u4fdd\u5b58\u5230\u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684 <code>open<\/code> \u51fd\u6570\u548c\u6587\u4ef6\u5bf9\u8c61\u7684 <code>write<\/code> \u65b9\u6cd5\u3002<strong>\u4f7f\u7528 <code>open<\/code> \u51fd\u6570\u3001\u4f7f\u7528\u6587\u4ef6\u5bf9\u8c61\u7684 <code>write<\/code> \u65b9\u6cd5\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff08with \u8bed\u53e5\uff09<\/strong> \u662f\u5b9e\u73b0\u8fd9\u4e00\u76ee\u7684\u7684\u5e38\u89c1\u65b9\u6cd5\u3002\u4ee5\u4e0b\u662f\u8be6\u7ec6\u63cf\u8ff0\u5176\u4e2d\u4e00\u79cd\u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><p><strong>\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff08with \u8bed\u53e5\uff09<\/strong>\uff1a\u8fd9\u662f\u63a8\u8350\u7684\u65b9\u5f0f\uff0c\u56e0\u4e3a\u5b83\u53ef\u4ee5\u786e\u4fdd\u6587\u4ef6\u5728\u4f7f\u7528\u5b8c\u540e\u88ab\u6b63\u786e\u5173\u95ed\uff0c\u5373\u4f7f\u5728\u5904\u7406\u8fc7\u7a0b\u4e2d\u53d1\u751f\u4e86\u9519\u8bef\u3002\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\u80fd\u81ea\u52a8\u7ba1\u7406\u8d44\u6e90\u7684\u5206\u914d\u548c\u91ca\u653e\uff0c\u4ece\u800c\u63d0\u9ad8\u4ee3\u7801\u7684\u5065\u58ee\u6027\u548c\u53ef\u8bfb\u6027\u3002<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello, world!\\n&#39;)<\/p>\n<p>    file.write(&#39;This is a test.\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u4f8b\u5b50\u4e2d\uff0c<code>open<\/code> \u51fd\u6570\u4ee5\u5199\u6a21\u5f0f\uff08&#39;w&#39;\uff09\u6253\u5f00\u540d\u4e3a <code>output.txt<\/code> \u7684\u6587\u4ef6\uff0c\u5982\u679c\u8be5\u6587\u4ef6\u4e0d\u5b58\u5728\uff0c\u5b83\u5c06\u88ab\u521b\u5efa\u3002\u5982\u679c\u6587\u4ef6\u5b58\u5728\uff0c\u5176\u5185\u5bb9\u5c06\u88ab\u8986\u76d6\u3002<code>file.write<\/code> \u65b9\u6cd5\u5c06\u5b57\u7b26\u4e32\u5199\u5165\u6587\u4ef6\u4e2d\u3002\u4ee5\u4e0b\u662f\u66f4\u8be6\u7ec6\u7684\u5185\u5bb9\u4ecb\u7ecd\u3002<\/p>\n<\/p>\n<h2><strong>\u4e00\u3001\u4f7f\u7528 open \u51fd\u6570<\/strong><\/h2>\n<p><p><code>open<\/code> \u51fd\u6570\u662f Python \u4e2d\u7528\u4e8e\u6253\u5f00\u6587\u4ef6\u7684\u5185\u7f6e\u51fd\u6570\u3002\u5176\u57fa\u672c\u8bed\u6cd5\u4e3a\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file_object = open(file_name, mode)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5176\u4e2d\uff0c<code>file_name<\/code> \u662f\u6587\u4ef6\u7684\u540d\u79f0\u6216\u8def\u5f84\uff0c<code>mode<\/code> \u662f\u6587\u4ef6\u7684\u6253\u5f00\u6a21\u5f0f\u3002\u5e38\u89c1\u7684\u6a21\u5f0f\u6709\uff1a<\/p>\n<\/p>\n<ul>\n<li><code>&#39;r&#39;<\/code>\uff1a\u53ea\u8bfb\u6a21\u5f0f\uff08\u9ed8\u8ba4\uff09<\/li>\n<li><code>&#39;w&#39;<\/code>\uff1a\u5199\u5165\u6a21\u5f0f\uff08\u4f1a\u8986\u76d6\u6587\u4ef6\uff09<\/li>\n<li><code>&#39;a&#39;<\/code>\uff1a\u8ffd\u52a0\u6a21\u5f0f\uff08\u5728\u6587\u4ef6\u672b\u5c3e\u6dfb\u52a0\u5185\u5bb9\uff09<\/li>\n<li><code>&#39;b&#39;<\/code>\uff1a\u4e8c\u8fdb\u5236\u6a21\u5f0f\uff08\u4e0e\u5176\u4ed6\u6a21\u5f0f\u7ed3\u5408\u4f7f\u7528\uff0c\u5982 <code>&#39;rb&#39;<\/code> \u6216 <code>&#39;wb&#39;<\/code>\uff09<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff0c\u4ee5\u4e0b\u4ee3\u7801\u6253\u5f00\u4e00\u4e2a\u6587\u4ef6\u8fdb\u884c\u5199\u5165\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file = open(&#39;output.txt&#39;, &#39;w&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u3001\u4f7f\u7528\u6587\u4ef6\u5bf9\u8c61\u7684 write \u65b9\u6cd5<\/strong><\/h2>\n<p><p>\u4e00\u65e6\u6587\u4ef6\u88ab\u6253\u5f00\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u6587\u4ef6\u5bf9\u8c61\u7684 <code>write<\/code> \u65b9\u6cd5\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">file.write(&#39;Hello, world!\\n&#39;)<\/p>\n<p>file.write(&#39;This is a test.\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u4e0a\u8ff0\u4ee3\u7801\u5c06\u5b57\u7b26\u4e32\u5199\u5165\u6587\u4ef6\uff0c\u6bcf\u4e2a\u5b57\u7b26\u4e32\u672b\u5c3e\u7684 <code>\\n<\/code> \u8868\u793a\u6362\u884c\u7b26\u3002<\/p>\n<\/p>\n<h2><strong>\u4e09\u3001\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff08with \u8bed\u53e5\uff09<\/strong><\/h2>\n<p><p>\u4f7f\u7528\u4e0a\u4e0b\u6587\u7ba1\u7406\u5668\uff08<code>with<\/code> \u8bed\u53e5\uff09\u662f\u7ba1\u7406\u6587\u4ef6\u7684\u6700\u4f73\u5b9e\u8df5\u3002\u8fd9\u4e0d\u4ec5\u7b80\u5316\u4e86\u4ee3\u7801\uff0c\u8fd8\u786e\u4fdd\u6587\u4ef6\u5728\u64cd\u4f5c\u5b8c\u6210\u540e\u88ab\u6b63\u786e\u5173\u95ed\u3002\u4ee5\u4e0b\u662f\u793a\u4f8b\u4ee3\u7801\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.write(&#39;Hello, world!\\n&#39;)<\/p>\n<p>    file.write(&#39;This is a test.\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728 <code>with<\/code> \u8bed\u53e5\u5757\u4e2d\uff0c<code>file<\/code> \u662f\u6587\u4ef6\u5bf9\u8c61\u3002\u79bb\u5f00 <code>with<\/code> \u8bed\u53e5\u5757\u65f6\uff0c\u6587\u4ef6\u4f1a\u81ea\u52a8\u5173\u95ed\u3002<\/p>\n<\/p>\n<h2><strong>\u56db\u3001\u5199\u5165\u591a\u884c\u6570\u636e<\/strong><\/h2>\n<p><p>\u5982\u679c\u4f60\u6709\u591a\u884c\u6570\u636e\u8981\u5199\u5165\u6587\u4ef6\uff0c\u53ef\u4ee5\u4f7f\u7528\u4e00\u4e2a\u5faa\u73af\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lines = [&#39;First line\\n&#39;, &#39;Second line\\n&#39;, &#39;Third line\\n&#39;]<\/p>\n<p>with open(&#39;output.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>\u6216\u8005\uff0c\u4f7f\u7528 <code>writelines<\/code> \u65b9\u6cd5\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">lines = [&#39;First line\\n&#39;, &#39;Second line\\n&#39;, &#39;Third line\\n&#39;]<\/p>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    file.writelines(lines)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e94\u3001\u5199\u5165\u4e8c\u8fdb\u5236\u6570\u636e<\/strong><\/h2>\n<p><p>\u5bf9\u4e8e\u4e8c\u8fdb\u5236\u6570\u636e\uff0c\u4f60\u9700\u8981\u4ee5\u4e8c\u8fdb\u5236\u6a21\u5f0f\u6253\u5f00\u6587\u4ef6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;output.bin&#39;, &#39;wb&#39;) as file:<\/p>\n<p>    file.write(b&#39;\\x00\\x01\\x02\\x03&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c<code>b&#39;\\x00\\x01\\x02\\x03&#39;<\/code> \u662f\u4e00\u4e2a\u5b57\u8282\u4e32\uff08<code>bytes<\/code> \u5bf9\u8c61\uff09\u3002<\/p>\n<\/p>\n<h2><strong>\u516d\u3001\u8bfb\u53d6\u6587\u4ef6\u5e76\u5199\u5165\u65b0\u6587\u4ef6<\/strong><\/h2>\n<p><p>\u4f60\u4e5f\u53ef\u4ee5\u5c06\u4e00\u4e2a\u6587\u4ef6\u7684\u5185\u5bb9\u8bfb\u53d6\u5e76\u5199\u5165\u5230\u53e6\u4e00\u4e2a\u6587\u4ef6\u4e2d\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;input.txt&#39;, &#39;r&#39;) as infile, open(&#39;output.txt&#39;, &#39;w&#39;) as outfile:<\/p>\n<p>    for line in infile:<\/p>\n<p>        outfile.write(line)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u8fd9\u4e2a\u793a\u4f8b\u4e2d\uff0c<code>input.txt<\/code> \u4e2d\u7684\u6bcf\u4e00\u884c\u88ab\u8bfb\u53d6\u5e76\u5199\u5165\u5230 <code>output.txt<\/code> \u4e2d\u3002<\/p>\n<\/p>\n<h2><strong>\u4e03\u3001\u8ffd\u52a0\u6a21\u5f0f<\/strong><\/h2>\n<p><p>\u5982\u679c\u4f60\u60f3\u5728\u6587\u4ef6\u672b\u5c3e\u6dfb\u52a0\u5185\u5bb9\u800c\u4e0d\u662f\u8986\u76d6\u73b0\u6709\u5185\u5bb9\uff0c\u53ef\u4ee5\u4f7f\u7528\u8ffd\u52a0\u6a21\u5f0f <code>&#39;a&#39;<\/code>\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;output.txt&#39;, &#39;a&#39;) as file:<\/p>\n<p>    file.write(&#39;Another line\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u516b\u3001\u9519\u8bef\u5904\u7406<\/strong><\/h2>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u65f6\uff0c\u53ef\u80fd\u4f1a\u53d1\u751f\u5404\u79cd\u9519\u8bef\uff08\u5982\u6587\u4ef6\u4e0d\u5b58\u5728\u3001\u6743\u9650\u4e0d\u8db3\u7b49\uff09\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528 <code>try...except<\/code> \u8bed\u53e5\u6765\u5904\u7406\u8fd9\u4e9b\u9519\u8bef\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">try:<\/p>\n<p>    with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>        file.write(&#39;Hello, world!\\n&#39;)<\/p>\n<p>except IOError as e:<\/p>\n<p>    print(f&quot;An IOError occurred: {e}&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e5d\u3001\u4f7f\u7528\u65e5\u5fd7\u8bb0\u5f55<\/strong><\/h2>\n<p><p>\u5728\u5b9e\u9645\u5e94\u7528\u4e2d\uff0c\u4f60\u53ef\u80fd\u5e0c\u671b\u5c06\u7a0b\u5e8f\u7684\u8f93\u51fa\u8bb0\u5f55\u5230\u65e5\u5fd7\u6587\u4ef6\u4e2d\u3002Python \u7684 <code>logging<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u5f3a\u5927\u7684\u65e5\u5fd7\u8bb0\u5f55\u529f\u80fd\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import logging<\/p>\n<p>logging.basicConfig(filename=&#39;app.log&#39;, level=logging.INFO)<\/p>\n<p>logging.info(&#39;This is an info message&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c\u65e5\u5fd7\u6d88\u606f\u5c06\u88ab\u5199\u5165 <code>app.log<\/code> \u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u3001\u4f7f\u7528\u6807\u51c6\u8f93\u51fa\u91cd\u5b9a\u5411<\/strong><\/h2>\n<p><p>\u6709\u65f6\u4f60\u53ef\u80fd\u60f3\u5c06\u6807\u51c6\u8f93\u51fa\u91cd\u5b9a\u5411\u5230\u6587\u4ef6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import sys<\/p>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    sys.stdout = file<\/p>\n<p>    print(&#39;This will be written to the file&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5728\u6b64\u793a\u4f8b\u4e2d\uff0c<code>print<\/code> \u51fd\u6570\u7684\u8f93\u51fa\u5c06\u88ab\u91cd\u5b9a\u5411\u5230 <code>output.txt<\/code> \u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u5341\u4e00\u3001\u8bfb\u53d6\u548c\u5199\u5165 CSV \u6587\u4ef6<\/strong><\/h2>\n<p><p>\u5bf9\u4e8e\u7ed3\u6784\u5316\u6570\u636e\uff0c\u5982 CSV \u6587\u4ef6\uff0cPython \u63d0\u4f9b\u4e86 <code>csv<\/code> \u6a21\u5757\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import csv<\/p>\n<h2><strong>\u5199\u5165 CSV \u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output.csv&#39;, &#39;w&#39;, newline=&#39;&#39;) as file:<\/p>\n<p>    writer = csv.writer(file)<\/p>\n<p>    writer.writerow([&#39;Name&#39;, &#39;Age&#39;])<\/p>\n<p>    writer.writerow([&#39;Alice&#39;, 30])<\/p>\n<p>    writer.writerow([&#39;Bob&#39;, 25])<\/p>\n<h2><strong>\u8bfb\u53d6 CSV \u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output.csv&#39;, &#39;r&#39;) 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<h2><strong>\u5341\u4e8c\u3001\u8bfb\u53d6\u548c\u5199\u5165 JSON \u6587\u4ef6<\/strong><\/h2>\n<p><p>\u5bf9\u4e8e JSON \u683c\u5f0f\u7684\u6570\u636e\uff0cPython \u63d0\u4f9b\u4e86 <code>json<\/code> \u6a21\u5757\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import json<\/p>\n<p>data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30}<\/p>\n<h2><strong>\u5199\u5165 JSON \u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output.json&#39;, &#39;w&#39;) as file:<\/p>\n<p>    json.dump(data, file)<\/p>\n<h2><strong>\u8bfb\u53d6 JSON \u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output.json&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = json.load(file)<\/p>\n<p>    print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u5341\u4e09\u3001\u5199\u5165\u591a\u79cd\u683c\u5f0f\u6587\u4ef6<\/strong><\/h2>\n<p><p>\u6709\u65f6\u5019\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5199\u5165\u4e0d\u540c\u683c\u5f0f\u7684\u6587\u4ef6\uff0c\u5982 XML\u3001YAML \u7b49\u3002Python \u63d0\u4f9b\u4e86\u76f8\u5e94\u7684\u5e93\uff1a<\/p>\n<\/p>\n<ul>\n<li><strong>XML<\/strong>\uff1a\u4f7f\u7528 <code>xml.etree.ElementTree<\/code> \u6a21\u5757<\/li>\n<li><strong>YAML<\/strong>\uff1a\u4f7f\u7528 <code>PyYAML<\/code> \u5e93\uff08\u9700\u8981\u5b89\u88c5 <code>pyyaml<\/code>\uff09<\/li>\n<\/ul>\n<p><p>\u4f8b\u5982\uff0c\u5199\u5165 XML \u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import xml.etree.ElementTree as ET<\/p>\n<p>root = ET.Element(&quot;root&quot;)<\/p>\n<p>child = ET.SubElement(root, &quot;child&quot;)<\/p>\n<p>child.text = &quot;Hello, world!&quot;<\/p>\n<p>tree = ET.ElementTree(root)<\/p>\n<p>tree.write(&quot;output.xml&quot;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u5199\u5165 YAML \u6587\u4ef6\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import yaml<\/p>\n<p>data = {&#39;name&#39;: &#39;Alice&#39;, &#39;age&#39;: 30}<\/p>\n<h2><strong>\u5199\u5165 YAML \u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output.yaml&#39;, &#39;w&#39;) as file:<\/p>\n<p>    yaml.dump(data, file)<\/p>\n<h2><strong>\u8bfb\u53d6 YAML \u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output.yaml&#39;, &#39;r&#39;) as file:<\/p>\n<p>    data = yaml.load(file, Loader=yaml.FullLoader)<\/p>\n<p>    print(data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u5341\u56db\u3001\u4f7f\u7528 pandas \u5e93\u5199\u5165\u6587\u4ef6<\/strong><\/h2>\n<p><p><code>pandas<\/code> \u5e93\u662f\u6570\u636e\u5206\u6790\u4e2d\u5e38\u7528\u7684\u5de5\u5177\uff0c\u5b83\u652f\u6301\u8bfb\u5199\u591a\u79cd\u683c\u5f0f\u7684\u6587\u4ef6\uff0c\u5982 CSV\u3001Excel\u3001JSON \u7b49\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import pandas as pd<\/p>\n<h2><strong>\u521b\u5efa DataFrame<\/strong><\/h2>\n<p>df = pd.DataFrame({&#39;Name&#39;: [&#39;Alice&#39;, &#39;Bob&#39;], &#39;Age&#39;: [30, 25]})<\/p>\n<h2><strong>\u5199\u5165 CSV \u6587\u4ef6<\/strong><\/h2>\n<p>df.to_csv(&#39;output.csv&#39;, index=False)<\/p>\n<h2><strong>\u5199\u5165 Excel \u6587\u4ef6<\/strong><\/h2>\n<p>df.to_excel(&#39;output.xlsx&#39;, index=False)<\/p>\n<h2><strong>\u5199\u5165 JSON \u6587\u4ef6<\/strong><\/h2>\n<p>df.to_json(&#39;output.json&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u5341\u4e94\u3001\u5199\u5165\u5927\u6587\u4ef6<\/strong><\/h2>\n<p><p>\u5f53\u5904\u7406\u5927\u6587\u4ef6\u65f6\uff0c\u9010\u884c\u5199\u5165\u662f\u4e00\u79cd\u5e38\u89c1\u7684\u65b9\u6cd5\uff0c\u4ee5\u907f\u514d\u5185\u5b58\u4e0d\u8db3\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">with open(&#39;large_output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    for i in range(1000000):<\/p>\n<p>        file.write(f&#39;Line {i}\\n&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u5341\u516d\u3001\u6587\u4ef6\u8def\u5f84\u7ba1\u7406<\/strong><\/h2>\n<p><p>\u5728\u5904\u7406\u6587\u4ef6\u65f6\uff0c\u7ba1\u7406\u6587\u4ef6\u8def\u5f84\u662f\u4e00\u4e2a\u91cd\u8981\u7684\u65b9\u9762\u3002Python \u7684 <code>os<\/code> \u548c <code>pathlib<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6587\u4ef6\u8def\u5f84\u64cd\u4f5c\u529f\u80fd\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>from pathlib import Path<\/p>\n<h2><strong>\u4f7f\u7528 os \u6a21\u5757<\/strong><\/h2>\n<p>file_path = os.path.join(&#39;directory&#39;, &#39;file.txt&#39;)<\/p>\n<h2><strong>\u4f7f\u7528 pathlib \u6a21\u5757<\/strong><\/h2>\n<p>file_path = Path(&#39;directory&#39;) \/ &#39;file.txt&#39;<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u5341\u4e03\u3001\u6587\u4ef6\u6743\u9650\u7ba1\u7406<\/strong><\/h2>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u8bbe\u7f6e\u6587\u4ef6\u6743\u9650\u3002Python \u7684 <code>os<\/code> \u6a21\u5757\u63d0\u4f9b\u4e86\u76f8\u5e94\u7684\u529f\u80fd\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import os<\/p>\n<p>file_path = &#39;output.txt&#39;<\/p>\n<p>os.chmod(file_path, 0o644)  # \u8bbe\u7f6e\u6587\u4ef6\u6743\u9650\u4e3a rw-r--r--<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u5341\u516b\u3001\u6587\u4ef6\u538b\u7f29\u548c\u89e3\u538b<\/strong><\/h2>\n<p><p>Python \u7684 <code>zipfile<\/code> \u548c <code>tarfile<\/code> \u6a21\u5757\u53ef\u4ee5\u7528\u6765\u5904\u7406\u538b\u7f29\u6587\u4ef6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import zipfile<\/p>\n<h2><strong>\u521b\u5efa ZIP \u6587\u4ef6<\/strong><\/h2>\n<p>with zipfile.ZipFile(&#39;output.zip&#39;, &#39;w&#39;) as zipf:<\/p>\n<p>    zipf.write(&#39;output.txt&#39;)<\/p>\n<h2><strong>\u89e3\u538b ZIP \u6587\u4ef6<\/strong><\/h2>\n<p>with zipfile.ZipFile(&#39;output.zip&#39;, &#39;r&#39;) as zipf:<\/p>\n<p>    zipf.extractall(&#39;extracted&#39;)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u5341\u4e5d\u3001\u6587\u4ef6\u52a0\u5bc6\u548c\u89e3\u5bc6<\/strong><\/h2>\n<p><p>\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\uff0c\u4f60\u53ef\u80fd\u9700\u8981\u5bf9\u6587\u4ef6\u8fdb\u884c\u52a0\u5bc6\u548c\u89e3\u5bc6\u3002Python \u7684 <code>cryptography<\/code> \u5e93\u53ef\u4ee5\u7528\u6765\u5904\u7406\u52a0\u5bc6\u548c\u89e3\u5bc6\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">from cryptography.fernet import Fernet<\/p>\n<h2><strong>\u751f\u6210\u5bc6\u94a5<\/strong><\/h2>\n<p>key = Fernet.generate_key()<\/p>\n<p>cipher = Fernet(key)<\/p>\n<h2><strong>\u52a0\u5bc6\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output.txt&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    encrypted_data = cipher.encrypt(file.read())<\/p>\n<p>with open(&#39;output.encrypted&#39;, &#39;wb&#39;) as file:<\/p>\n<p>    file.write(encrypted_data)<\/p>\n<h2><strong>\u89e3\u5bc6\u6587\u4ef6<\/strong><\/h2>\n<p>with open(&#39;output.encrypted&#39;, &#39;rb&#39;) as file:<\/p>\n<p>    decrypted_data = cipher.decrypt(file.read())<\/p>\n<p>with open(&#39;output_decrypted.txt&#39;, &#39;wb&#39;) as file:<\/p>\n<p>    file.write(decrypted_data)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<h2><strong>\u4e8c\u5341\u3001\u6587\u4ef6\u9501\u5b9a<\/strong><\/h2>\n<p><p>\u5728\u5904\u7406\u591a\u8fdb\u7a0b\u6216\u591a\u7ebf\u7a0b\u73af\u5883\u65f6\uff0c\u6587\u4ef6\u9501\u5b9a\u53ef\u4ee5\u9632\u6b62\u7ade\u4e89\u6761\u4ef6\u3002Python \u7684 <code>fcntl<\/code> \u6a21\u5757\uff08\u4ec5\u9002\u7528\u4e8e Unix \u7cfb\u7edf\uff09\u548c <code>portalocker<\/code> \u5e93\u53ef\u4ee5\u7528\u6765\u5b9e\u73b0\u6587\u4ef6\u9501\u5b9a\u3002\u4f8b\u5982\uff1a<\/p>\n<\/p>\n<p><pre><code class=\"language-python\">import portalocker<\/p>\n<p>with open(&#39;output.txt&#39;, &#39;w&#39;) as file:<\/p>\n<p>    portalocker.lock(file, portalocker.LOCK_EX)<\/p>\n<p>    file.write(&#39;Hello, world!\\n&#39;)<\/p>\n<p>    portalocker.unlock(file)<\/p>\n<p><\/code><\/pre>\n<\/p>\n<p><p>\u7efc\u4e0a\u6240\u8ff0\uff0cPython \u63d0\u4f9b\u4e86\u4e30\u5bcc\u7684\u6587\u4ef6\u64cd\u4f5c\u529f\u80fd\uff0c\u4ece\u57fa\u672c\u7684\u6587\u4ef6\u8bfb\u5199\u5230\u9ad8\u7ea7\u7684\u6587\u4ef6\u52a0\u5bc6\u3001\u538b\u7f29\u3001\u9501\u5b9a\u7b49\u3002\u638c\u63e1\u8fd9\u4e9b\u6280\u5de7\u53ef\u4ee5\u5e2e\u52a9\u4f60\u5728\u5b9e\u9645\u9879\u76ee\u4e2d\u66f4\u52a0\u9ad8\u6548\u5730\u5904\u7406\u6587\u4ef6\u3002<\/p>\n<\/p>\n<h2><strong>\u76f8\u5173\u95ee\u7b54FAQs\uff1a<\/strong><\/h2>\n<p> <strong>\u5982\u4f55\u5728Python\u4e2d\u5c06\u6570\u636e\u5199\u5165\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\u4ef6\uff0c\u5e76\u4f7f\u7528<code>write()<\/code>\u6216<code>writelines()<\/code>\u65b9\u6cd5\u5c06\u6570\u636e\u5199\u5165\u6587\u4ef6\u3002\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">with open(&#39;output.txt&#39;, &#39;w&#39;) as file:\n    file.write(&#39;Hello, World!\\n&#39;)\n    file.writelines([&#39;Line 1\\n&#39;, &#39;Line 2\\n&#39;])\n<\/code><\/pre>\n<p>\u4e0a\u8ff0\u4ee3\u7801\u521b\u5efa\u4e00\u4e2a\u540d\u4e3a<code>output.txt<\/code>\u7684\u6587\u4ef6\uff0c\u5e76\u5728\u5176\u4e2d\u5199\u5165\u4e24\u884c\u6587\u672c\u3002<\/p>\n<p><strong>\u53ef\u4ee5\u5c06\u54ea\u4e9b\u7c7b\u578b\u7684\u6570\u636e\u8f93\u51fa\u5230\u6587\u4ef6\uff1f<\/strong><br \/>Python\u652f\u6301\u591a\u79cd\u6570\u636e\u7c7b\u578b\u7684\u8f93\u51fa\uff0c\u5305\u62ec\u5b57\u7b26\u4e32\u3001\u6570\u5b57\u3001\u5217\u8868\u548c\u5b57\u5178\u7b49\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\u4f7f\u7528<code>json<\/code>\u6a21\u5757\u53ef\u4ee5\u65b9\u4fbf\u5730\u5c06\u5b57\u5178\u6216\u5217\u8868\u7b49\u8f6c\u6362\u4e3aJSON\u683c\u5f0f\u5e76\u5199\u5165\u6587\u4ef6\u3002\u793a\u4f8b\uff1a  <\/p>\n<pre><code class=\"language-python\">import json\n\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\u5728\u5199\u5165\u6587\u4ef6\u65f6\u5904\u7406\u5f02\u5e38\uff1f<\/strong><br \/>\u5728\u6587\u4ef6\u64cd\u4f5c\u4e2d\uff0c\u5904\u7406\u5f02\u5e38\u975e\u5e38\u91cd\u8981\uff0c\u4ee5\u907f\u514d\u7a0b\u5e8f\u5d29\u6e83\u3002\u5728Python\u4e2d\uff0c\u53ef\u4ee5\u4f7f\u7528<code>try...except<\/code>\u8bed\u53e5\u6765\u6355\u83b7\u53ef\u80fd\u53d1\u751f\u7684\u5f02\u5e38\uff0c\u5e76\u91c7\u53d6\u76f8\u5e94\u7684\u63aa\u65bd\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(&#39;Writing data to file.&#39;)\nexcept IOError as e:\n    print(f&#39;An error occurred: {e}&#39;)\n<\/code><\/pre>\n<p>\u8fd9\u6837\u53ef\u4ee5\u786e\u4fdd\u5982\u679c\u5728\u5199\u5165\u6587\u4ef6\u65f6\u53d1\u751f\u9519\u8bef\uff0c\u7a0b\u5e8f\u5c06\u6b63\u5e38\u5904\u7406\u5e76\u7ed9\u51fa\u63d0\u793a\uff0c\u800c\u4e0d\u662f\u76f4\u63a5\u7ec8\u6b62\u8fd0\u884c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"\u8981\u5c06 Python \u8f93\u51fa\u4fdd\u5b58\u5230\u6587\u4ef6\uff0c\u4f60\u53ef\u4ee5\u4f7f\u7528\u5185\u7f6e\u7684 open \u51fd\u6570\u548c\u6587\u4ef6\u5bf9\u8c61\u7684 write \u65b9\u6cd5\u3002\u4f7f\u7528 op [&hellip;]","protected":false},"author":3,"featured_media":1182828,"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\/1182819"}],"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=1182819"}],"version-history":[{"count":"1","href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182819\/revisions"}],"predecessor-version":[{"id":1182830,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/posts\/1182819\/revisions\/1182830"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media\/1182828"}],"wp:attachment":[{"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/media?parent=1182819"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/categories?post=1182819"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/docs.pingcode.com\/wp-json\/wp\/v2\/tags?post=1182819"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}